是否可以在全球范围内声明一个提升线程
Is it possible to declare a boost thread globally?
您好,我想在全球范围内声明一个Boost线程并以后初始化。所以:
#include <iostream>
using namespace std;
boost::thread t;
void some_func()
{
printf("hello worldn");
}
int main()
{
t(some_func);
return 0;
}
它正在返回有关初始化的错误。
error: no match for call to ‘(boost::thread) (void (&)())
那么我该如何初始化?
edit ::我想这样做的原因是因为我想根据条件产生线程。因此,在伪代码中:
if (cond A satisfied)
spawn thread_A
if (cond B satisfied)
spawn thread_B
// Do some stuff
if (cond B satisfied)
thread_B.join()
if (cond A satisfied)
thread_A.join()
如果线程没有全局范围,那么我不能这样做
t
的默认构造函数初始化对象没有活动线程。为了解决此问题,您必须执行互换。
boost::thread(&some_func).swap(t);
或rvalue的分配(在C 11中):
t = boost::thread(&some_func);
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- 在另一个线程中调用luaL_error会引发qWarning
- C++一个线程如何正确通信其任务已完成?
- 最佳做法是从另一个线程访问 qml 中的Q_PROPERTY
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 如何制作一个只能在一个线程上同时执行的函数?
- 结束另一个线程中使用的对象的生存期
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 一个线程等待多个线程事件
- 从不同进程中的另一个线程挂起/恢复线程或进程
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 计时器是否从另一个线程启动?
- 互斥,Windows 10,c ++,在一个线程上获取,在另一个线程上发布
- Qt 在另一个线程中无限循环
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- 当只有一个线程主要使用该对象而其他线程很少使用它时,如何最小化该对象的互斥锁锁定?
- 从另一个线程发出信号是否安全?
- AMQP-CPP,libev >停止ev_loop来自另一个线程