Re: [Boost-Users] Re: Newbie needs pointer to currently executing thread.
Threads in a thread_group - derived pool are waiting on a condition. Condition gets notify_one(). How do I get id/pointer for the
I'm implementing the Leader/Followers thread pool pattern - reference
"Pattern-Oriented Software Architecture Volume 2" (Schmidt, et al).
When a thread is released via notify_one(), it is [assumed to be] in the
"leader" role and stores it's id. A sanity check should be performed to
verify that there is no other thread in the leader role (i.e.,
current_leader is "none") before the thread stores it's id there.
When one of the event sources is signaled and the leader is unblocked, it
must, among other tasks, demote itself (i.e., set the current_leader value
to "none")
and promote one of the followers by notify_one() before continuing to
service the request. Because of the potential for race conditions,
(especially in the context of future program updates, modifications, etc.)
I should enforce the rule that only the leader can promote a follower.
Something along the lines of: current_leader == Thread::this().
"dmoore99atwork"
was released? I've got a feeling I'm gonna be *REAL* embarrassed when I find out, but I can't figure it out.
Thanks for any help.
Dick, In general, condition variables (both the Boost.Threads version and the POSIX version) don't tell the "notifier" which thread woke up. That's not necessarily a problem, though. A thread pool usually implies a set of threads that are equivalent in some way, even running the same thread function. Referring to the example at: http://www.boost.org/libs/thread/doc/condition.html#Example, Take a look the the receive() function. while (buffered == 0) buffer_not_empty.wait(lk); You might have three threads which reach this point when buffered==0 (meaning the buffer is empty). When a new sender in a fourth thread adds something to the buffer and calls notify_one(), exactly one of the three threads waiting on the condition will wake up and do some work. You won't care which thread wakes up, because any one of them will be prepared to do the necessary work. If this doesn't get you unstuck, please post more details, in particular why you feel it is important to know which thread "woke up" Regards, Dave Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
I'm implementing the Leader/Followers thread pool pattern - reference "Pattern-Oriented Software Architecture Volume 2" (Schmidt, et al).
When a thread is released via notify_one(), it is [assumed to be] in the "leader" role and stores it's id. A sanity check should be performed to verify that there is no other thread in the leader role (i.e., current_leader is "none") before the thread stores it's id there.
When one of the event sources is signaled and the leader is unblocked, it must, among other tasks, demote itself (i.e., set the current_leader value to "none") and promote one of the followers by notify_one() before continuing to service the request. Because of the potential for race conditions, (especially in the context of future program updates, modifications, etc.) I should enforce the rule that only the leader can promote a follower. Something along the lines of: current_leader == Thread::this().
I disagree (about the race condition). Your condition variable is usually used in conjunction with a mutex which protects your shared state's invariants. boost::mutex m; boost::condition c; Invariant: leader != NONE Pseudo-code for leader releasing boost::mutex::scoped_lock l(m); // lock mutex if(TimeForANewLeader) { leader=NONE; c.notify_one(); // Will signal one waiter..... // finish task - MUTEX IS STILL HELD! // } l.unlock(); // Only now will a waiter return from wait(); Pseudeo-code for follower waiting for signal: boost::mutex::scoped_lock l(m); // lock mutex while(Not TimeForANewLeader) { c.wait(m); // Waiting automically releases AND reacquires // the mutex } // When we get here, we know we hold the mutex. // Follower is responsible for "self-promotion" leader=ME; // Start my work as a leader. An important note is that if we reach the while() statement and it IS time for a new leader, then we never wait, we already hold the mutex, and it is safe for us to promote ourselves anyways! Note that for the time being, you'll have to go outside of Boost.Threads for the leader=ME statement because thread objects aren't copyable so leader=boost::thread(); doesn't work to get the current thread. Bill Kempf has indicated that some form of portable thread_id s are coming in a future version. Hope this helps... Dave
participants (2)
-
Dave Moore
-
dick.bridges@tais.com