Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何设置线程数组的动态大小_C++_Arrays_Multithreading_C++11 - Fatal编程技术网

C++ 如何设置线程数组的动态大小

C++ 如何设置线程数组的动态大小,c++,arrays,multithreading,c++11,C++,Arrays,Multithreading,C++11,如您所见,我想创建几个线程。现在线程的数量取决于我从控制台获得的命令行参数,因此基本上不需要创建动态线程,但在C++11中创建线程类对象数组需要给定常量大小,这就是我的问题所在,因为它只接受“num_threads”在初始化时不使用变量(即使用文字)。 类似:静态常量int num\u threads=10; 但不适用于以下情况:静态常量int num_threads=stoi(argv[1]); int main(int argc, char *argv[]) { if(argc!=2)

如您所见,我想创建几个线程。现在线程的数量取决于我从控制台获得的命令行参数,因此基本上不需要创建动态线程,但在C++11中创建线程类对象数组需要给定常量大小,这就是我的问题所在,因为它只接受“num_threads”在初始化时不使用变量(即使用文字)。
类似:
静态常量int num\u threads=10;

但不适用于以下情况:
静态常量int num_threads=stoi(argv[1]);

int main(int argc, char *argv[])
{
  if(argc!=2)
  {
     cout << "\n Invalid arguments \n";
     return 0;
  }

  static  const int num_threads = 10;// stoi(argv[1]);//
  thread t[num_threads];


 //Launch a group of threads
 for (int i = 0; i < num_threads; ++i) 
 {
    t[i] = std::thread(call_from_main_to_connect_info_disconnect, i);
 }

 std::cout << "Launched from the main\n";

//Join the threads with the main thread
 for (int i = 0; i < num_threads; ++i) 
 {
    t[i].join();
 }

 getchar();
 return err;

}
intmain(intargc,char*argv[])
{
如果(argc!=2)
{
cout您可以使用:

std::thread *t = new std::thread[num_threads];
或者使用
std::vector

std::vector<std::thread> t(num_threads);
std::vector t(num_线程);

<代码>使用动态数组,或者因为C++上C++使用“代码> STD::向量< /代码>,当你认为“动态数组”时,你应该下一步思考。天才DimChtz…为我工作。对于我来说,我应该想到这个概念是白痴的。使用新的操作符U将在堆上分配并将地址传递给指针(类线程的类型).我说的对吗?如果没有请告诉我。