Re: Make a small function thread safe

2011-12-19 Thread ting
On Dec 16, 8:21 am, Brad Tilley wrote: > A thread locks the function on entrance and then releases it on exit. > What is the equivalent way to do this in Python? I'm not sure if this applies in your case, but much of the time, you can use thread local storage, rather thread locking, in order to m

Re: Make a small function thread safe

2011-12-18 Thread Ian Kelly
On Sun, Dec 18, 2011 at 6:27 PM, Tim Delaney wrote: > On 18 December 2011 19:52, RangerElf wrote: >> >> Which is why the original .acquire() ... .release() idiom was wrong, this >> would better express the intent: >> >> try: >>  lock.acquire() >>  shared_container.append(...) >> finally: >>  lock

Re: Make a small function thread safe

2011-12-18 Thread Tim Delaney
On 18 December 2011 19:52, RangerElf wrote: > Which is why the original .acquire() ... .release() idiom was wrong, this > would better express the intent: > > try: > lock.acquire() > shared_container.append(...) > finally: > lock.release() > No - this is very bad. The lock must be acquired ou

Re: Make a small function thread safe

2011-12-18 Thread RangerElf
Which is why the original .acquire() ... .release() idiom was wrong, this would better express the intent: try: lock.acquire() shared_container.append(...) finally: lock.release() But like everyone mentions, the with statement takes care of all that in a more readable and compact fashion.

Re: Make a small function thread safe

2011-12-16 Thread John Nagle
On 12/16/2011 2:08 PM, Lie Ryan wrote: On 12/17/2011 01:30 AM, Brad Tilley wrote: Or perhaps run should look like this instead: def run(t): lock.acquire() shared_container.append(t.name ) lock.release() That seems a bit barbaric to me, not sure. change that to: def run(t): wi

Re: Make a small function thread safe

2011-12-16 Thread Lie Ryan
On 12/17/2011 01:30 AM, Brad Tilley wrote: Or perhaps run should look like this instead: def run(t): lock.acquire() shared_container.append(t.name ) lock.release() That seems a bit barbaric to me, not sure. change that to: def run(t): with l

Re: Make a small function thread safe

2011-12-16 Thread Tim Delaney
On 17 December 2011 02:05, Brad Tilley wrote: > On Dec 16, 9:36 am, Tim Wintle wrote: > > > should be: > > def run(t): > > with lock: > > shared_container.append(t.name) > > > > (or lock.acquire() and lock.release() as you mentioned) > > > Thanks Tim. The with state

Re: Make a small function thread safe

2011-12-16 Thread Brad Tilley
On Dec 16, 9:36 am, Tim Wintle wrote: > should be: >       def run(t): >           with lock: >               shared_container.append(t.name) > > (or lock.acquire() and lock.release() as you mentioned) Thanks Tim. The with statement is closer to the C++ code (IMO) more so than the explicit acqu

Re: Make a small function thread safe

2011-12-16 Thread Tim Wintle
On Fri, 2011-12-16 at 09:24 -0500, Brad Tilley wrote: > So something like this then: > > import threading > > shared_container = [] > lock = threading.Lock() > > class thread_example( threading.Thread ): > > def __init__( self ): > threading.Thread.__init__ (self) > > def run(t

Re: Make a small function thread safe

2011-12-16 Thread Brad Tilley
On Fri, Dec 16, 2011 at 9:24 AM, Brad Tilley wrote: > > > On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote: > >> On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: >> > 107 void increment_counter( unsigned int& counter ) >> > 108 { >> > 109 boost::mutex::scoped

Re: Make a small function thread safe

2011-12-16 Thread Brad Tilley
On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote: > On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: > > 107 void increment_counter( unsigned int& counter ) > > 108 { > > 109 boost::mutex::scoped_lock lock( counter_lock ); > > 110 ++counter; >

Re: Make a small function thread safe

2011-12-16 Thread Tim Wintle
On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: > 107 void increment_counter( unsigned int& counter ) > 108 { > 109 boost::mutex::scoped_lock lock( counter_lock ); > 110 ++counter; > 111 } with counter_lock: counter += 1 ... wher

Make a small function thread safe

2011-12-16 Thread Brad Tilley
Hey guys, I have a C++ function that I'd like to replicate (as closely as possible) in Python. Here's an example: 107 void increment_counter( unsigned int& counter ) 108 { 109 boost::mutex::scoped_lock lock( counter_lock ); 110 ++counter; 111