Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Locks in Multithreading
#1
Hello Pythoners,

I am having trouble with some multithreading.
My goal is it to read a given list and start a thread for each element of this list, in this thread i want to sort the elements into a new empty list.

my problem is, that no matter how i set up my locks, the sorting works for the first few elements, but after that numbers, which schuld be in the middle in the list, just get appended to the end of the list

the code below for example gives me for s1 [3, 5, 7, 9, 122, 222, 213, 210]
What did i do wrong, how do i know where to put the locks ?

import threading
def parsort(v,s,lock):
    i=0
    if s==[]:
        s.append(v)
    else:
        while i < len(s):
            if s[i] > v and not v in s:
                print(v)
                s.insert(i,v)
            elif not v in s:
                s.append(v)
            i=i+1

#Mainprogramm
s1=[]
l=[5,3,7,9,122,222,213,210]
lock=threading.RLock()

for  i in l :
    sort=threading.Thread(target=parsort,args=(i,s1,lock))
    sort.start()
    for j in l:
        sort.join()


print(s1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  multithreading Hanyx 4 1,336 Jul-29-2022, 07:28 AM
Last Post: Larz60+
Question Problems with variables in multithreading Wombaz 2 1,339 Mar-08-2022, 03:32 PM
Last Post: Wombaz
  Multithreading question amadeok 0 1,787 Oct-17-2020, 12:54 PM
Last Post: amadeok
  How can i add multithreading in this example WoodyWoodpecker1 3 2,518 Aug-11-2020, 05:30 PM
Last Post: deanhystad
  matplotlib multithreading catosp 0 2,956 Jul-03-2020, 09:33 AM
Last Post: catosp
  Multithreading dynamically syncronism Rodrigo 0 1,541 Nov-08-2019, 02:33 AM
Last Post: Rodrigo
  multithreading issue with output mr_byte31 4 3,215 Sep-11-2019, 12:04 PM
Last Post: stullis
  Multithreading alternative MartinV279 1 2,802 Aug-01-2019, 11:41 PM
Last Post: scidam
  using locks in multithreading in python3 srm 2 3,678 Jul-13-2019, 11:35 AM
Last Post: noisefloor
  Multithreading in a loop valtih 3 16,410 Aug-03-2017, 08:20 PM
Last Post: valtih

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020