在不同的线程上调用和执行函数
Calling and executing function on a different thread
假设,我有一个这样的程序代码:
#include <iostream>
#include <Windows.h>
#include <tbb/tbb.h>
void SomeFunction()
{
// do something
}
void MyThread(int arg)
{
std::cout << "This is a thread functionn" << std::endl;
for (int i = 0; i < 10000; i++)
{
arg++;
Sleep(1);
}
SomeFunction();
}
int main ()
{
tbb::tbb_thread pMyThread = tbb::tbb_thread(MyThread, 3);
pMyThread.join();
return 0;
}
从上面我们可以看到main()在不同的线程pMyThread上调用MyThread()。MyThread() 正在调用 SomeFunction()。现在,SomeFunction()(或由MyThread()调用的任何其他函数)也会在pMyThread上执行吗?谢谢。
是的,从线程的主函数发出的任何函数调用都将存在于该线程的私有堆栈中。
相关文章:
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 为什么即使调用了析构函数,C++11 中的分离线程也可以执行
- 无法调用成员函数,尝试正确执行此操作仍然失败
- 不执行任何操作的函数调用C++
- 在 c++ 托管和异步运行中调用 c# 可执行文件
- 调用函数一次用于动态链接库,一次从可执行文件调用函数
- 如何执行参数化构造函数的调用?
- 当再次触发信号时,从Qt插槽执行的功能被第二次调用时会发生什么?
- 在可变函数调用中执行一次语句
- 如何在派生类中不显式调用base::func()的情况下从基类执行虚拟函数
- 只有一个可执行文件即可进行跨平台 SIMD 调用?
- 调用函数来创建 WinAPI 按钮不会执行任何操作
- 从 PHP 调用已编译的C++可执行文件来创建套接字
- 如何让派生类在调用基类之前执行操作?
- 在 MySQL 连接器C++ API 中使用一个函数调用执行多个查询的正确方法是什么?
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 编译器是否对所有内联函数调用执行相同的操作
- setxkbmap 从系统调用执行时返回 65280
- 在C++中,我们如何用有效的X(Args..)语法调用执行函数的所有内容
- 如何为函数调用执行令牌粘贴