[英]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)
,但是如果这个问题对您来说很困难,那么myThreads.push_back(thread)
可能是个好主意,因为它们会复杂化很喜欢的一个节目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.