create thread - passing arguments

前端 未结 3 995
清歌不尽
清歌不尽 2021-02-05 14:32

I am attempting on creating multiple threads that each thread calculates a prime. I am trying to pass a second argument to a function using thread create. It keeps throwing up e

3条回答
  •  隐瞒了意图╮
    2021-02-05 15:08

    In case of std::thread, the user can pass arguments to the thread function in the following method

    std::thread(funcName,arg1,arg2);

    for instance,

    //for a thread function, 
    void threadFunction(int x,int y){
       std::cout << x << y << std::endl;
    }
    
    // u can pass x and y values as below
    std::thread mTimerThread;
    mTimerThread = std::thread(threadFunction,1,12);
    

提交回复
热议问题