【发布时间】:2014-01-10 20:58:26
【问题描述】:
我在谷歌上搜索了 2 多个小时,但我找不到我的问题的答案:
我在 Windows 8 (x86) 上使用 C++(Visual Studio Express 2012)
我想在进程的主线程上从我的工作人员 std::thread 运行一个方法。我尝试使用它们的 std::thread::id 创建两个线程的引用,然后交换它们,但我无法创建引用。
从我的辅助线程调用以下方法:
[..]
void run(bool *running, thread::id _mainThreadId)
{
while(*running)
{
thread mainThread(_mainThreadId);
thread thisThread(this_thread::get_id());
thisThread.swap(mainThread);
//mainThreads work:
[..]
thisThread.swap(mainThread);
//thisThread again
}
}
但是当我尝试编译我的项目时,出现了一个错误,显示构造函数 thread(thread::id) 不存在。
我的问题:
有没有更好的方法在另一个线程上运行代码? 有没有办法使用它的 id 来获取对线程的引用?
提前致谢!
PS: 如有语法错误,请见谅。我来自德国。 :)
【问题讨论】:
标签: c++ multithreading visual-studio-2012 windows-8 std