[IronPython] concurrent list access

Curt Hagenlocher curt at hagenlocher.org
Mon Oct 6 07:41:39 CEST 2008


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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081005/c90c7aee/attachment.html>


More information about the Ironpython-users mailing list