中断线程提振 [英] Interrupting boost thread
本文介绍了中断线程提振的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够中断一个线程,如下。
I would like to be able to interrupt a thread as follows.
void mainThread(char* cmd)
{
if (!strcmp(cmd, "start"))
boost::thread thrd(sender); //start thread
if (!strcmp(cmd, "stop"))
thrd.interrupt(); // doesn't work, because thrd is undefined here
}
thrd.interrupt()是不可能的,因为 THRD 的对象是不确定的,当我试图打断它。我该如何解决这个问题?
thrd.interrupt() is not possible because thrd object is undefined when I try to interrupt it. How can I fix this?
推荐答案
使用的<一个href=\"http://www.boost.org/doc/libs/1_47_0/doc/html/thread/thread_management.html#thread.thread_management.thread.move_assignment\"相对=nofollow>移动赋值操作符:
void mainThread(char* cmd)
{
boost::thread thrd;
if (!strcmp(cmd, "start"))
thrd = boost::thread(sender); //start thread
if (!strcmp(cmd, "stop"))
thrd.interrupt();
}
这篇关于中断线程提振的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文