
Hi: I have two threads that are started by a class created by the applications main thread. Can the thread function be a private member of that class or does it need to be public? When the application exits I need the threads to be notified and exit. What is the best way of handling this? I need this to be cross platform but in windows I would have used a waitforSingleObject and signalled the event when the main thread wants to exit. Could I use a condition for this, and do a do while loop waiting on the condition to be notified, if I do this do I need a mutex just for the condition? Any help appreciated. Cheers Sean.

I have two threads that are started by a class created by the applications main thread.
Can the thread function be a private member of that class or does it need to be public?
The thread doesn't care whether they are private or public, but if you want to create the thread from outside the class, then the thread function should be visible and accessible at the point where you reference it.
When the application exits I need the threads to be notified and exit. What is the best way of handling this? I need this to be cross platform but in windows I would have used a waitforSingleObject and signalled the event when the main thread wants to exit.
Use thread::join() member function to wait on threads to complete. You can notify threads with condition_variable, or use some applicative methods, depending on what these threads actually do.
Could I use a condition for this, and do a do while loop waiting on the condition to be notified, if I do this do I need a mutex just for the condition?
Take a look at the following example to see how to handle condition_variable: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-...

Hi: If I do a thread.join, will the main thread continue, i.e I have a while loop in the main thread waiting for a windows message, if I put thread.join in there I don't want the loop to stop. Cheers Sean. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Igor R Sent: 24 November 2010 12:00 To: boost-users@lists.boost.org Subject: Re: [Boost-users] boost thread query
I have two threads that are started by a class created by the applications main thread.
Can the thread function be a private member of that class or does it need to be public?
The thread doesn't care whether they are private or public, but if you want to create the thread from outside the class, then the thread function should be visible and accessible at the point where you reference it.
When the application exits I need the threads to be notified and exit. What is the best way of handling this? I need this to be cross platform but in windows I would have used a waitforSingleObject and signalled the event when the main thread wants to exit.
Use thread::join() member function to wait on threads to complete. You can notify threads with condition_variable, or use some applicative methods, depending on what these threads actually do.
Could I use a condition for this, and do a do while loop waiting on the condition to be notified, if I do this do I need a mutex just for the condition?
Take a look at the following example to see how to handle condition_variable: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-... _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

If I do a thread.join, will the main thread continue, i.e I have a while loop in the main thread waiting for a windows message, if I put thread.join in there I don't want the loop to stop.
No, thread.join() blocks the calling thread just like WinAPI WaitForSingleObject() that you mentioned in your OP.
participants (2)
-
Igor R
-
Sean Farrow