Is there a way to reuse pthreads?

主宰稳场 提交于 2019-12-21 05:46:06

问题


I have a function that is called millions of times, and the work done by this function is multithreaded. Here is the function:

void functionCalledSoManyTimes()
{
  for (int i = 0; i < NUM_OF_THREADS; i++)
  {
    pthread_create(&threads[i], &attr, thread_work_function, (void *)&thread_data[i]);
  }
  // wait
}

I'm creating the threads each time the function is called, and I give each thread its data struct (that's been set once at the beginning of the algorithm) to use in the thread_work_function. The thread_work_functionsimply processes a series of arrays, and the thread_data struct contains pointers to those arrays and the indices that each thread is responsible for.

Although multithreading the algorithm in this way did improve the performance by more than 20%, my profiling shows that the repetitive calls to pthread_create are causing a significant overhead.

My question is: Is there a way to achieve my goal without calling pthread_create each time the function is called?

Problem Solved.

Thank you guys, I really appreciate your help! I've written a solution here using your tips.


回答1:


Just start a fixed set of threads and use an inter-thread communication system (ring buffer, for instance) to pass the data to process.




回答2:


Solving the problem gracefully is not so easy. You can use static storage for a thread pool, but then what happens if functionCalledSoManyTimes itself can be called from multiple threads? It's not a good design.

What I would do to handle this sort of situation is create a thread-local storage key with pthread_key_create on the first call (using pthread_once), and store your thread-pool there with pthread_setspecific the first time functionCalledSoManyTimes gets called in a given thread. You can provide a destructor function to pthread_key_create which will get called when the the thread exists, and this function can then be responsible for signaling the worker threads in the thread pool to terminate themselves (via pthread_cancel or some other mechanism).



来源:https://stackoverflow.com/questions/12268514/is-there-a-way-to-reuse-pthreads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!