multithreading

Tim Peters tim.one at comcast.net
Wed May 29 23:24:53 EDT 2002


[Mark Hammond]
> You can invoke whatever you like, but it doesn't change anything for
> anyone else ;)
>
> I believe advocating the Queue module as the "one way" is naive.  The
> Queue module is very useful, and indeed has solved many threading
> problems in an elegant way for me - however, in my experience, it has
> been used in less than 50% of the times I have needed mutli-threaded
> synchronization.

That's because you're not thinking straight.  Here's an ugly critical
section with a dangerous primitive lock:

    alock.acquire()
    do something critical
    alock.release()

Here's a beautiful critical section with a robust Queue q:

    q.get()
    do something critical
    q.put("this may look like a string, but it's a lock")

See?  Just make sure every critical section tries to get from the queue
before putting something on it, and make sure nobody puts something on the
queue when they shouldn't.  Also put something on q right after you create
it, so the first q.get() doesn't block forever.  That's much easier than
remembering not to acquire a lock right after you create one.

> If-your-only-tool-is-a-hammer-everything-starts-looking-like-a-thumb ly

I can't imagine what you're on about.

when-all-you-have-are-thumbs-everything-looks-like-an-ass<wink>-ly y'rs
    - tim






More information about the Python-list mailing list