相关疑难解决方法(0)

通过同步延长线程的生命周期(C++ 11)

我有一个程序,其函数将指针作为arg和main.主要是创建n个线程,每个线程根据传递的内容在不同的内存区域运行arg.然后连接线程,主要在区域之间执行一些数据混合,并创建n个新线程,这些线程执行与旧线程相同的操作.

为了改进程序,我想保持线程活着,消除创建它们所需的长时间.线程应在主要工作时休眠,并在必须再次出现时通知.同样,主要应该在线程工作时等待,就像连接一样.

我无法最终实现这一点,总是陷入僵局.

简单的基线代码,任何有关如何修改它的提示将非常感激

#include <thread>
#include <climits>

...

void myfunc(void * p) {
  do_something(p);
}

int main(){
  void * myp[n_threads] {a_location, another_location,...};
  std::thread mythread[n_threads];
  for (unsigned long int j=0; j < ULONG_MAX; j++) {
    for (unsigned int i=0; i < n_threads; i++) {
      mythread[i] = std::thread(myfunc, myp[i]);
    }
    for (unsigned int i=0; i < n_threads; i++) {
      mythread[i].join();
    }
    mix_data(myp); 
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading thread-safety c++11

15
推荐指数
3
解决办法
1万
查看次数

标签 统计

c++ ×1

c++11 ×1

multithreading ×1

thread-safety ×1