如何在函数中传递值并将函数放在线程中

How to pass a value in a function and put the function in a thread?

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

我想创建一个具有两个函数的cpp程序。

void func(int num){
 //Assume there are contents here
}
int main(){
    int num;
    cin >> num
    ap(num);
}

上面的代码在执行时有效。但是我想在线程中使用我的函数func(int num)

thread t1(func);
t1.join; 

当我在线程中使用我的 func() 时,in 不再执行。这就是为什么我无法继续编写第二个函数的原因。将值传递给函数以便可以在线程中使用的正确方法是什么?

我建议您查看此链接:https://www.tutorialcup.com/cplusplus/multithreading.htm它有很多例子和很多理论来阅读和理解线程和多线程。