我有一个工作队列,我想建立一个4个线程的池,我可以把我的工作.我所坚持的是如何制作线程并在没有工作的情况下让它们暂停.
JOB QUEUE | job1 | job2 | job3 | job4 | ..
THREAD POOL | thread1 | thread2 | thread3 | thread4 |
Run Code Online (Sandbox Code Playgroud)
要创建我目前处于初始化点的线程:
for (t=0; t<num_of_threads; t++){
pthread_create(&(threads[t]), NULL, doSth2, NULL);
}
Run Code Online (Sandbox Code Playgroud)
num_of_threads = 4且doSth2是一个内部没有任何内容的函数.所以一旦我创建了4个线程并且完成了doSth2,我怎么能给他们新的工作呢,而不会杀死他们?
I'm trying to understand some of the basics of using POSIX pthreads. The kind of thing I need to do (eventually) is parallelize some computations, using a thread pool model. At present I want to ensure I have a very basic sense for how the POSIX pthread model works. So I'm trying to create the simplest thread pool that's general enough to do the kinds of things I want to do. There will be some shared memory, an input queue, …