在不同的线程上调用和执行函数

Calling and executing function on a different thread

本文关键字:调用 执行 函数 线程      更新时间:2023-10-16

假设,我有一个这样的程序代码:

    #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上执行吗?谢谢。

是的,从线程的主函数发出的任何函数调用都将存在于该线程的私有堆栈中。