Enthusiastic person
Enthusiastic person

Reputation: 1

Invoking thread function later

I had a question in C? Is it possible to create a thread and invoke the thread function later whenever it is required? Probable using a thread_start routine whenever it is required. Otherwise, if i use the pthread_create the thread will be created multiple times and have to be handled carefully. Please help me. Thanks in advance.

Upvotes: 0

Views: 88

Answers (1)

kaishu
kaishu

Reputation: 59

You can use semaphores, just block thread function in while loop:

    while(1){
        sem_wait(my_semaphore);
        code_that_needs_to_be_done_in_thread;
    }

..and whenever you need your thread,just simply signal it:

    sem_post(my_semaphore);
    ...
    other_code;
    sem_post(my_semaphore);
    ...

Upvotes: 2

Related Questions