Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing pthread pool

Tags:

c

threadpool

I need to implement a thread pool using pthreads. I could not move forward. I found similar question here But that still does not clarify my question. My question is once a thread runs to its termination can I use it again? Another way of putting this question is, How does a thread return to its thread pool after running a task to its completion. Could anyone point me to some simple pthread pool article? My confusion arises mainly because I have little bit of java background. I read somewhere we cannot call start() on thread second time once it terminates.

like image 224
FourOfAKind Avatar asked Aug 08 '26 18:08

FourOfAKind


1 Answers

My question is once a thread runs to its termination can I use it again?

Yes, that's the purpose of the pool, to reuse threads instead of destroying them.

How does a thread return to its thread pool.

By trying to get another element from the queue. Doing it in a loop is one way.

Here is what every thread does in my implementation (this is the actual function used with pthread_create):

static void *
_tp_worker(void *arg)
{
    /* ... */

    /* Wait until tasks is available. */
    while (!queue_get(pool->pend_q, &t_ptr)) {
        /* And then execute it. */
    }
}
like image 169
cnicutar Avatar answered Aug 11 '26 12:08

cnicutar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!