Search code examples
clinuxposix

pthreads running in reverse order


I have this very simple code:

void *myfunc (void *variable);

int main(int argc, char *argv[])
{
    pthread_t thread1, thread2;
    char *msg1 = "First thread";
    char *msg2 = "Second thread";
    int ret1, ret2;

    ret1 = pthread_create(&thread1, NULL, myfunc, (void *) msg1);
    ret2 = pthread_create(&thread2, NULL, myfunc, (void *) msg2);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    printf("First thread ret1 = %d\n", ret1);
    printf("Second thread ret2 = %d\n", ret2);
    return 0;
}

void *myfunc (void *variable)
{
    char *msg;
    msg = (char *) variable;
    printf("%s\n", msg);
}

And this is the result I'm consistently getting:

Second thread 
First thread
First thread ret1 = 0
Second thread ret2 = 0

In the code I create the first thread before, but the second thread appear to runs the first. As far as I know you can't control which thread runs first, but I've run the program multiple times, with a "for" loop, and it's always the same result, it doesn't look random. Is there any way I can make sure the thread I create the first runs first?


Solution

  • Is there any way I can make sure the thread I create the first runs first?

    Sure there is. Serialize the other threads after the first one with e.g., a semaphore (error checking omitted):

    #include <pthread.h>
    
    #include <stdio.h>
    #include <semaphore.h>
    sem_t semaphore;
    void *myfunc (void *variable)
    {
        char *msg;
        msg = variable;
        /*if not first, wait on the semaphore and the post to it
          otherwise just post so the other threads may start*/
        if('F'!=*msg) 
            sem_wait(&semaphore);
        printf("%s\n", msg);
        sem_post(&semaphore);
    }
    
    int main(int argc, char *argv[])
    {
        pthread_t thread1, thread2;
        char *msg1 = "First thread";
        char *msg2 = "Second thread";
        int ret1, ret2;
    
        sem_init(&semaphore,0,0);
    
        ret1 = pthread_create(&thread1, NULL, myfunc, (void *) msg1);
        ret2 = pthread_create(&thread2, NULL, myfunc, (void *) msg2);
    
        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);
    
        printf("First thread ret1 = %d\n", ret1);
        printf("Second thread ret2 = %d\n", ret2);
        return 0;
    }