<div dir="ltr">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:<br>
1) If the source of the assignment is user-defined type, incur the extra expense of copying that collection into a local list.<br>2) Hold a lock for the entire time that the target list is mutated.<br><br>This would involve a performance hit when the source is a user-defined type -- oh, well.<br>
<br>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 <a href="http://www.codeplex.com/IronPython/WorkItem/List.aspx">http://www.codeplex.com/IronPython/WorkItem/List.aspx</a><br>
<br><div class="gmail_quote">On Sun, Oct 5, 2008 at 9:09 PM, Severin <span dir="ltr"><seob@gmx.ch></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr"><div class="gmail_quote"><div dir="ltr"><span style="border-collapse: collapse;">Hello,<div><br></div><div>I have a problem with lists.</div><div><br></div><div>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.</div>
<div><br></div><div>for some reason it doesn't work on ironpython (2.0B5 on Vista, quad-core) sometimes.</div><div><br></div><div>so:</div><div>- can my concurrent list access corrupt the list? (especially the slice operator)</div>
<div>- can anybody see another problem here?</div><div><br></div><div>any hint is appreciated</div><div><br></div><div>Severin</div><div><br></div><div>here is the code:</div><div>-----</div><div><div>import threading</div>
<div><br></div><div>threads = []</div><div><br></div><div>def partition(array, begin, end):</div><div> while begin < end:</div><div> while begin < end:</div><div> if array[begin] > array[end]:</div>
<div> (array[begin], array[end]) = (array[end], array[begin])</div><div> break</div><div> end -= 1</div><div> while begin < end:</div><div> if array[begin] > array[end]:</div>
<div> (array[begin], array[end]) = (array[end], array[begin])</div><div> break</div><div> begin += 1</div><div> return begin</div><div><br></div><div><br></div>
<div>
def quicksort(array, begin, end):</div><div> if begin < end:</div><div> </div><div> if end - begin <= 2**12:</div><div> #print "%d %d" % (begin, end)</div><div>
sorted = [x for x in array[begin:end]]</div>
<div> sorted.sort()</div><div> array[begin:end] = sorted</div><div> else:</div><div> </div><div> split = partition(array, begin, end-1)</div><div><br></div>
<div> thread = threading.Thread(target=quicksort, args=(array, begin, split,))</div><div> thread.start()</div><div> threads.append(thread)</div><div><br></div><div> thread = threading.Thread(target=quicksort, args=(array, split+1, end))</div>
<div> thread.start()</div><div> threads.append(thread)</div><div> </div><div><br></div><div><br></div><div>if __name__ == '__main__':</div><div> </div><div> import random</div>
<div> </div><div> # settings</div><div> n = 2**14</div><div> </div><div> # setup</div><div> l = range(n)</div><div> random.shuffle(l)</div><div> </div><div> # sort</div><div> quicksort(l, 0, len(l))</div>
<div><br></div><div> # join</div><div> for thread in threads:</div><div> thread.join()</div><div> </div><div> # test</div><div> for i in range(n):</div><div> if i != l[i]:</div><div> print 'failure %d != %d' % (i, l[i])</div>
<div> </div><div> print 'done'</div></div></span></div>
</div><br></div>
<br>_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
<br></blockquote></div><br></div>