如何在C++中使用多线程
How do I use multithreading in C++?
我需要知道如何从一个数字中制作未知数量的线程,这是我的代码:
void callWritePrimesMultipleThreads(int begin, int end, string filePath, int N){
ofstream file;
file.open(filePath);
thread myThreads[N];//cant make it because N isnt a constant value.
}
我真的想过制作一个数组,但我不能,因为我收到的数字对于函数来说是未知的,有什么方法可以在这个函数中乘以线程吗?
尝试在函数中使用std::vector
:
void callWritePrimesMultipleThreads(int begin, int end, string filePath, int N){
ofstream file;
file.open(filePath);
vector<std::thread> myThreads;
}
然后使用 push_back(thread t)
将线程添加到向量中,如下所示:myThreads.push_back(thread)
,但是如果这个问题对您来说很难,那么远离线程一段时间可能是个好主意,因为它们使程序非常复杂。
相关文章:
- 在C++中使用cURL和多线程
- 多线程双缓冲区
- 为什么我的多线程作业队列崩溃
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 为什么一个向量上的多线程操作很慢
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 全局变量 多读取器 一个写入器多线程安全?
- boost::文件系统::recursive_directory_iterator多线程安全
- 如何阻止TensorFlow的多线程
- 如何在多线程中正确使用unique_ptr进行多态性?
- 并发/多线程:是否可以以这种方式生成相同的输出?
- sigwait() 在多线程程序中不起作用
- 多线程程序中出现意外的内存泄漏
- 静态 constexpr 类成员变量对多线程读取是否安全?
- 多线程比没有线程C++慢
- 具有 C++11 多线程的特征库
- 通过安装信号处理程序关闭多线程应用程序
- 成员变量在多线程 C++ 时自行更改
- 尝试从头开始实现Leetcode的FizzBuzz多线程问题。收到"libc++abi.dylib: terminating"错误