[IronPython] concurrent list access
Severin
seob at gmx.ch
Mon Oct 6 10:39:48 CEST 2008
I think you are right. Not using the slice operator doesn't give the wrong
output anymore.
Take a look at the implementation it's clear that this doesn't work like
this because the list is copied in 3 steps and the lock is released in
between.
this means that concurrent slice updates results in loosing some of the new
values.
On Mon, Oct 6, 2008 at 7:41 AM, Curt Hagenlocher <curt at hagenlocher.org>wrote:
> Looking at the code, I believe that it's possible for slice-assignment to
> cause a problem with multiple threads. (In fact, there's a comment there
> that says "// race is tolerable...".) I suspect that the operation could be
> made more thread-safe by incorporating the following logic:
> 1) If the source of the assignment is user-defined type, incur the extra
> expense of copying that collection into a local list.
> 2) Hold a lock for the entire time that the target list is mutated.
>
> This would involve a performance hit when the source is a user-defined type
> -- oh, well.
>
> You can try to confirm this hypothesis by replacing the slice-assignment
> with a loop that replaces the values one at a time. If making this change
> prevents the problem, then the slice-assignment is indeed at fault and you
> should file a problem report on CodePlex at
> http://www.codeplex.com/IronPython/WorkItem/List.aspx
>
> On Sun, Oct 5, 2008 at 9:09 PM, Severin <seob at gmx.ch> wrote:
>
>> Hello,
>> I have a problem with lists.
>>
>> Taking a quick look to the IronPython list implementation they intend to
>> be thread safe (I guess). now I made a quicksort example that sorts a list
>> in place using threads. its a divide and conquer approach and at a certain
>> level the list ist just sorted using the sort function.
>>
>> for some reason it doesn't work on ironpython (2.0B5 on Vista, quad-core)
>> sometimes.
>>
>> so:
>> - can my concurrent list access corrupt the list? (especially the slice
>> operator)
>> - can anybody see another problem here?
>>
>> any hint is appreciated
>>
>> Severin
>>
>> here is the code:
>> -----
>> import threading
>>
>> threads = []
>>
>> def partition(array, begin, end):
>> while begin < end:
>> while begin < end:
>> if array[begin] > array[end]:
>> (array[begin], array[end]) = (array[end],
>> array[begin])
>> break
>> end -= 1
>> while begin < end:
>> if array[begin] > array[end]:
>> (array[begin], array[end]) = (array[end],
>> array[begin])
>> break
>> begin += 1
>> return begin
>>
>>
>> def quicksort(array, begin, end):
>> if begin < end:
>>
>> if end - begin <= 2**12:
>> #print "%d %d" % (begin, end)
>> sorted = [x for x in array[begin:end]]
>> sorted.sort()
>> array[begin:end] = sorted
>> else:
>>
>> split = partition(array, begin, end-1)
>>
>> thread = threading.Thread(target=quicksort, args=(array,
>> begin, split,))
>> thread.start()
>> threads.append(thread)
>>
>> thread = threading.Thread(target=quicksort, args=(array,
>> split+1, end))
>> thread.start()
>> threads.append(thread)
>>
>>
>>
>> if __name__ == '__main__':
>>
>> import random
>>
>> # settings
>> n = 2**14
>>
>> # setup
>> l = range(n)
>> random.shuffle(l)
>>
>> # sort
>> quicksort(l, 0, len(l))
>>
>> # join
>> for thread in threads:
>> thread.join()
>>
>> # test
>> for i in range(n):
>> if i != l[i]:
>> print 'failure %d != %d' % (i, l[i])
>>
>> print 'done'
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081006/b9369db0/attachment.html>
More information about the Ironpython-users
mailing list