pthread_create() 和 pthread_join() 函数在同一个循环中不起作用

pthread_create() and pthread_join() function doesn't work in the same loop

我编写了一些创建多个线程的代码,并 运行 在特定内核上设置这些线程。但是如果我将 pthread_create()pthread_join() 函数放在同一个循环中,运行 代码就会出现问题。

for(TN=0;TN<NUM_THREADS;TN++)
{ 

  pthread_create(&thread[TN],NULL,(void*) &hardAffinity,(void*)&CPU[TN]);           

  pthread_join(thread[TN],NULL);
}

问题是,如果我想 运行 在 cpu 的单独核心上,上面的代码不起作用。我认为这是因为 create 和 join 方法在同一个循环中。只有当我将每个方法放在一个单独的循环中时它才有效

解决此问题的一种方法是在单独的循环中调用 pthread_create() 和 pthread_join() 函数

for(TN=0;TN<NUM_THREADS;TN++)

 { 

   pthread_create(&thread[TN],NULL,(void*) &hardAffinity,(void*)&CPU[TN]);           

 }

 for(TN=0;TN<NUM_THREADS;TN++)

 {

   pthread_join(thread[TN],NULL);

 }