【问题标题】:How to pass a value in a function and put the function in a thread?如何在函数中传递值并将函数放入线程中?
【发布时间】:2016-01-22 15:20:18
【问题描述】:

我想创建一个有两个功能的 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 不再执行。这就是为什么我不能继续编写我的第二个函数的原因。将值传递给函数以便在线程中使用的正确方法是什么?

【问题讨论】:

  • 读取文件并显示内容。使用一个线程阅读,另一个线程用于写作。在线程之间共享一个或两个缓冲区。

标签: c++ multithreading function


【解决方案1】:

我建议你看看这个链接:https://www.tutorialcup.com/cplusplus/multithreading.htm 它包含许多示例和大量理论,可供阅读和理解线程和多线程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2014-09-22
    • 2020-07-17
    • 1970-01-01
    • 2022-11-25
    • 2012-11-20
    相关资源
    最近更新 更多