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.