[Tutor] Threads
Gonçalo Rodrigues
op73418 at mail.telepac.pt
Tue Nov 16 13:56:10 CET 2004
Terry Carroll wrote:
> On Mon, 15 Nov 2004, Gonçalo Rodrigues wrote:
>
>
>>Terry Carroll wrote:
>>
>>
>>>What's the ordinary way of telling a thread that it's no longer needed?
>>
>>Use the queue to pass an object that the thread can recognize as an
>>order to shut down.
>
>
> Okay, but that was one possibility I'd mentioned. The problem is where
> you have multiple consumer threads reading the Queue. Only the first
> consumer will get the magic shutdown object.
>
> What's the solution? Keep count of the number of consumer threads and
> enqueue one shutdown object per consumer? (Seems icky; there has to be a
> less smelly way.)
>
After I replied I went to sleep and the thread seems to have gained much
life -- my inbox has a lot of Re: Threads posts!.
I think it was Alan Munroe that talked of one solution - once a Thread
gets a shutdown event it pushes another into the queue and then proceeds
obediently and orderly to shutdown.
If you are ready to code just a little bit more, add one extra level of
indirection. Have all your Thread's waiting on their own private queue.
This way you can send messages to any Thread in particular. To organize
work among a bunch of these threads you can have an extra Thread that
acts like a post mail, it receives "packets of work" and distributes
them among the worker threads.
Queues are really wonderful and if you can shoehorn your thread
framework by using them you are saving yourself a lot of trouble.
With my best regards,
G. Rodrigues
More information about the Tutor
mailing list