stopping a boost::thread

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

stopping a boost::thread

xargon-2
Hi everyone,

I am trying to use the boost multithreading library. I am having a
problem as the join() function for the thread never returns. I am using
a boolean variable to indicate when the thread should stop running.
Here is basically a stub of what I am doing:


//........................................
bool m_RunThread = false;
scoped_ptr myThread;
//.......................................

void MyObject::StartThread()
{
m_RunThread = true;
myThread.reset(new boost::thread(boost::bind
(&MyObject::MyThreadFunc, this)));
}

void MyObject::StopThread()
{
m_RunThread = false;
myThread.join(); // this function never returns!

... Never gets here....

}

void MyObject::MyThreadFunc()
{
while (m_RunThread)
{
// do something
}
}


However, the join() function in StopThread never returns. I think that
using a variable to synchronize this is probably not a good idea. Any
thoughts and suggestions?

Cheers,

K

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: stopping a boost::thread

Frank Mori Hess
On Monday 06 February 2006 04:37 pm, [hidden email]
wrote:

> Date: Mon, 6 Feb 2006 20:28:49 +0000 (UTC)
> From: xargon <[hidden email]>
> Subject: [Boost-users] stopping a boost::thread
> To: [hidden email]
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=us-ascii
>
> Hi everyone,
>
> I am trying to use the boost multithreading library. I am having a
> problem as the join() function for the thread never returns. I am using
> a boolean variable to indicate when the thread should stop running.
> Here is basically a stub of what I am doing:
>
>
> //........................................
> bool m_RunThread = false;
It's because m_RunThread isn't volatile.

--
Frank

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: stopping a boost::thread

xargon-2
Frank Hess <frank.hess <at> nist.gov> writes:


> It's because m_RunThread isn't volatile.

Sorry about that. Had forgotten to put it in my original post. The m_RunThread
is declared as volatile.




_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: stopping a boost::thread

xargon-2
In reply to this post by Frank Mori Hess
Got it! I was calling a function that was updating a UI object through a call
to SendMessage! Of course, there was a deadlock.



_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users