0

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?

Sergio
  • 53
  • 5
  • Threads are running *independently* from each other. Once you've created them you have no control over in which order they start, or how they will run. If you need the threads to run in a specific order, you need to explicitly specify that order using synchronization primitives (like condition variables or semaphores or such). – Some programmer dude Jan 01 '19 at 20:17
  • 1
    Also, the `ret1` and `ret2` variables contain the success/failure of the `pthread_create` calls only, not what the thread "returns" – Some programmer dude Jan 01 '19 at 20:19
  • Maybe also think about the differences between "arbitrary", "unpredictable", "random", and "implementation-dependent". Anyhow, in the end, you simply cannot depend on any specific behavior. – tripleee Jan 01 '19 at 20:21

1 Answers1

0

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;
}
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142