如何在函数中传递值并将函数放在线程中
How to pass a value in a function and put the function in a thread?
我想创建一个具有两个函数的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它有很多例子和很多理论来阅读和理解线程和多线程。
相关文章:
- 如何将元素添加到数组的线程安全函数?
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- C++使用params创建线程函数会导致转换错误
- 为什么std::async使用同一个线程运行函数
- 使用 std::string () const 函数启动线程或未来
- C++线程中,没有重载函数接受 X 参数
- 如何从线程中的不同模块调用函数?
- 如何在调用析构函数时优雅地停止/销毁带有阻塞调用C++线程?
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 如何将类的成员函数传递给线程
- 是否可以创建一个从不同类调用函数的线程?
- 如何制作一个只能在一个线程上同时执行的函数?
- 启动类函数作为失去引用的线程
- 为什么即使调用了析构函数,C++11 中的分离线程也可以执行
- 非静态成员失败的线程调用函数
- 线程调用的函数对对象删除是否安全?
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 类成员在构造函数(线程)之后更改地址
- C++无限依次运行两个函数(线程)
- c++将各种参数传递给父类构造函数(线程c++11)