Re: [Boost-Users] stop a thread
From: "hlh771
" How to stop(or cancel) a thread? Suppose I have a main thread and a worker thread, the worker thread does something looply,and when the main thread get a message indicating the process should exit, I want stop the worker thread,how to do that?I have read other thread library,their thread class supply a method called stop or cancel which stops the thread. Their Implementation is:the thread class has a data member indicating whether the thread should stop,and the thread function looply check the data member whether the thread should stop.Generally the data member's type is a bool or Event. Is it the best way to stop the thread? How the boost to deal with this? Thanks in advance.
Today you should use a flag and a condition/mutex combination. Direct support for cancellation is being provided in a future release. William E. Kempf wekempf@cox.net
From: "hlh771
" How to stop(or cancel) a thread? Suppose I have a main thread and a worker thread, the worker thread does something looply,and when
main thread get a message indicating the process should exit, I want stop the worker thread,how to do that?I have read other thread library,their thread class supply a method called stop or cancel which stops the thread. Their Implementation is:the thread class has a data member indicating whether the thread should stop,and the thread function looply check the data member whether the thread should stop.Generally the data member's type is a bool or Event. Is it the best way to stop the thread? How the boost to deal with this? Thanks in advance.
Today you should use a flag and a condition/mutex combination. Direct support for cancellation is being provided in a future release.
William E. Kempf wekempf@c... Thanks a lot. Can I only use a bool flag?Although more than one thread may access
--- In Boost-Users@yahoogroups.com, William E. Kempf
William E. Kempf wrote:
From: "hlh771
" How to stop(or cancel) a thread? Suppose I have a main thread and a worker thread, the worker thread does something looply,and when the main thread get a message indicating the process should exit, I want stop the worker thread,how to do that?I have read other thread library,their thread class supply a method called stop or cancel which stops the thread. Their Implementation is:the thread class has a data member indicating whether the thread should stop,and the thread function looply check the data member whether the thread should stop.Generally the data member's type is a bool or Event. Is it the best way to stop the thread? How the boost to deal with this? Thanks in advance.
Today you should use a flag and a condition/mutex combination. Direct support for cancellation is being provided in a future release.
Actually, this works fine for ordinary loops. The problem I am facing is how to stop a thread which may sit in blocking system calls like reading from a (blocking) socket. Is there any "standard" way to solve this problem? Now, I am using nonblocking sockets, implementing "timeouts" myself, but it just doesn't feel right... Aki
William E. Kempf wekempf@cox.net
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/
Aschwin Gopalan said:
William E. Kempf wrote:
From: "hlh771
" How to stop(or cancel) a thread? Suppose I have a main thread and a worker thread, the worker thread does something looply,and when the main thread get a message indicating the process should exit, I want stop the worker thread,how to do that?I have read other thread library,their thread class supply a method called stop or cancel which stops the thread. Their Implementation is:the thread class has a data member indicating whether the thread should stop,and the thread function looply check the data member whether the thread should stop.Generally the data member's type is a bool or Event. Is it the best way to stop the thread? How the boost to deal with this? Thanks in advance.
Today you should use a flag and a condition/mutex combination. Direct support for cancellation is being provided in a future release.
Actually, this works fine for ordinary loops. The problem I am facing is how to stop a thread which may sit in blocking system calls like reading from a (blocking) socket. Is there any "standard" way to solve this problem? Now, I am using nonblocking sockets, implementing "timeouts" myself, but it just doesn't feel right...
No, there's no "standard" way to do this, and it's one of the most difficult things I'm facing with adding cancellation to Boost.Threads. For specific cases there's usually a solution. For example, in your case you could use select() with a file descripter created for the sole purpose of waking up the thread when you want to cancel it. But solutions like this are not necessarily completely portable, and don't apply to every use case where there's a blocking call. William E. Kempf wekempf@cox.net
From: "hlh771
" How to stop(or cancel) a thread? Suppose I have a main thread and a worker thread, the worker thread does something looply,and when
--- In Boost-Users@yahoogroups.com, William E. Kempf
main thread get a message indicating the process should exit, I want stop the worker thread,how to do that?I have read other thread library,their thread class supply a method called stop or cancel which stops the thread. Their Implementation is:the thread class has a data member indicating whether the thread should stop,and the thread function looply check the data member whether the thread should stop.Generally the data member's type is a bool or Event. Is it the best way to stop the thread? How the boost to deal with this? Thanks in advance.
Today you should use a flag and a condition/mutex combination. Direct support for cancellation is being provided in a future release.
William E. Kempf wekempf@c... Thanks.I'm not very good at multithread programming,but I like boost.threads. How to use a flag and a condition/mutex combination to cancel a thread?Why use condition?Could you show me a simple example? Expect your answer.
participants (3)
-
Aschwin Gopalan
-
hlh771 <hlh771@yahoo.com.cn>
-
William E. Kempf