0

I have a simple code:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMBER_OF_THREADS 10
void *print_hello_world(void *tid)
{
        printf("Hello World. Greetings from thread %d\n", tid);
        pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
        pthread_t threads[NUMBER_OF_THREADS];
        int status, i;
        for(i=0; i < NUMBER_OF_THREADS; i++) {
                printf("Main here.Creating thread %d\n",i);
                status = pthread_create(&threads[i], NULL, print_hello_world, (void *)i);
                if (status != 0) {
                        printf("Oops. pthread create returned error code %d\n", status);
                        exit(-1);
                }
        }
        exit(NULL);
}

After i did

gcc -pthread -o main threads.c

and run it, i got such output:

Main here. Creating thread 0
Main here. Creating thread 1
Main here. Creating thread 2
Hello World. Greetings from thread 1
Main here. Creating thread 3
Hello World. Greetings from thread 2
Main here. Creating thread 4
Hello World. Greetings from thread 0
Main here. Creating thread 5
Hello World. Greetings from thread 4
Hello World. Greetings from thread 3
Main here. Creating thread 6
Hello World. Greetings from thread 5
Main here. Creating thread 7
Main here. Creating thread 8
Main here. Creating thread 9
Hello World. Greetings from thread 6
Hello World. Greetings from thread 7
Hello World. Greetings from thread 8

But when i run that code on my server, i got that output:

Main here.Creating thread 0
Main here.Creating thread 1
Main here.Creating thread 2
Main here.Creating thread 3
Main here.Creating thread 4
Main here.Creating thread 5
Main here.Creating thread 6
Main here.Creating thread 7
Main here.Creating thread 8
Main here.Creating thread 9

Why did i get different output? I use Ubuntu-18.04 WLS2 on my local pc and Ubuntu 19.10 on my server and gcc 9.2.1 20191008.

  • 2
    Though @ggorlen is right that ordering is non-deterministic, that's not what's going on here. You're launching all these threads in the loop, but then exiting the entire program before you've given the threads a chance to run and say hello. Just how many get to say hello (sometimes zero) depends on performance and chance and the phase of the moon. AFTER the loop you need to wait for these threads to complete properly, THEN your `main` can exit. – Steve Friedl Jan 12 '20 at 19:05
  • @ggloren, i understand that, but i launched program on my server more than 100 times and got same result every time. – Иван Шкурко Jan 12 '20 at 19:06
  • 1
    Sure, but on your server it's able to launch all the threads *really fast* and then exit before any of them have a chance to actually get started. Also, it's possible that the I/O buffering is done slightly differently on the two platforms. – Steve Friedl Jan 12 '20 at 19:07
  • 1
    This is completely a hack, but try adding `sleep(1);` after the loop (and just before the `exit`) and see if that changes the behavior. This is not the proper way to wait for threads, but it may help you understand more what's going on. – Steve Friedl Jan 12 '20 at 19:10
  • Like Steve said, try using `pthread_join`, `sleep` or `sched_yield` in the main to give the threads a chance to run. It's unclear what you're trying to accomplish, though. Saying "I ran it 100 times and it never did x" doesn't really prove or disprove anything about non-deterministic programs. – ggorlen Jan 12 '20 at 19:10
  • 1
    "Non-deterministic" is not the same as *random*, and definitely not the same as *uniformly* random. That some non-deterministic behavior manifests the same way in all your observations does not imply that you can rely on it to do the same the next time, or under different circumstances. – John Bollinger Jan 12 '20 at 19:10

0 Answers0