I've often wondered if the CLR Interlocked methods work with
IronPython. It seems that Interlocked.Increment works fine with module
globals (see attached script.) Whether it works fine in general I
don't know. Does anybody know anything about this?

-Dan

>ipy interlockedinc.py
UnsafeCount: -32081
SafeCount: 0

>ipy interlockedinc.py
UnsafeCount: -27749
SafeCount: 0

>ipy interlockedinc.py
UnsafeCount: -37339
SafeCount: 0

>ipy interlockedinc.py
UnsafeCount: -39461
SafeCount: 0

>ipy interlockedinc.py
UnsafeCount: 23175
SafeCount: 0
from System.Threading import Thread, ThreadStart, Interlocked

def main():
    thread1 = Thread(ThreadStart(thread_method))
    thread2 = Thread(ThreadStart(thread_method))
    thread3 = Thread(ThreadStart(thread_method))
    thread1.Start()
    thread2.Start()
    thread3.Start()
    thread1.Join()
    thread2.Join()
    thread3.Join()
       
    print 'UnsafeCount: %d\nSafeCount: %d' % (UnsafeCount, SafeCount)

UnsafeCount = 0
SafeCount = 0    
    
def thread_method():
    global UnsafeCount, SafeCount
    for i in xrange(100000):
        UnsafeCount += 1
        Interlocked.Increment(SafeCount)
        
    for i in xrange(100000):
        UnsafeCount -= 1
        Interlocked.Decrement(SafeCount)       
        
if __name__ == '__main__':
    main()

_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to