简体   繁体   中英

C , Pthreads - How to re-execute function or start_routine specified

I am new to C programming and I am taking a course for it. A task given to me is given below

  1. Create an Operator ( One operator as one thread)
  2. Create 10 Retailers initially (One thread each)
  3. Create 10 Customers initially (One thread each)

Future task requires the threads created before. There are more task such as Retailers communicate through operator to customers but that is not what I am going to ask

Here's some of my code

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREAD 21


void* operator (void* arg) {
     //Logic here
}

void* retailer(void *arg) {
    //Logic here
}

void* customer(void* arg) {
   //Logic here
}

main() {
    pthread_t threads[NUM_THREAD];

    int i = 0;
    pthread_create(&threads[0],NULL , operator , NULL);
    printf("Operator created.\n");

    for ( i = 1 ; i < 11 ; i++) {
        pthread_create(&threads[i], NULL , retailers ,NULL);

    }

    for (i = 11 ; i < NUM_THREAD ; i++) {
        pthread_create(&threads[i], NULL , customers , NULL);

    }

}

Expected Output is

Operator created.
Retailer 1 Created.
...
Retailer 10 Created.
Customer 1 Created.
...
Customer 10 Created.
..The rest of output are the action between Operator , retailers and customers which involves semaphores and other stuff

I have searched on the internet, I don't have any ideas on how to "reuse" the threads after their creation. When I create a thread, it should start executing the function specified in pthread_create. However, after the function is executed , it simply loses the meaning of task 1,2,3 as the tasks after task 3 requires these threads.

To be clear, here's my question

  1. How can I create a thread and keep it alive so that I can reuse the function later?
  2. Is it a good approach to put if...else in thread's start_routine?

In answer to your first question, thread functions generally consist of start-up code, a long-running loop of some description, and termination code, such asthe following pseudo-code, as a rough illustration:

def retailer:
    initialise thread-local stuff
    while termination condition not met:
        do some processing
    terminate thread-local stuff

So a thread will continue to run until such time as some piece of code (such as a CTRL-C signal handler, or the main thread when the user tells it to stop) indicates it should stop.

Note that the proper use of protecting resources shared among threads (such as checking of termination conditions and any inter-thread communication involved in "do some processing") is inherent in the above code, I haven't explicitly mentioned it but it should be done.

For your second question, you can pretty much put any code you want in the thread function, just as if it were a normal single threaded piece of code.

Threading introduces certain aspects you don't normally have to concern yourself with, such as race conditions, thread local storage or thread synchronisation (mutexes, condition variables and such), but you're allowed to use the normal C control flow stuff like if and while without concern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM