#include <stdio.h>
#include <pthread.h>
void *thread_func(void *arg)
{
printf("hello, world \n");
return 0;
}
int main(void)
{
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_func, NULL);
pthread_create(&t2, NULL, thread_func, NULL);
printf("t1 = %d\n",t1);
printf("t2 = %d\n",t2);
return 0;
}
The above program creates two threads, where each thread prints "Hello World".
So, according to my understanding, "Hello world" should print a maximum of 2 times.
However, when running the same program several times (back to back), there are scripts in which "Hello world" is printed more than 2 times. So I don’t understand how it prints an unexpected number of times?
The following is the output:
[rr@ar ~]$ ./a.out
t1 = 1290651392
t2 = 1282258688
hello, world
hello, world
[rr@ar ~]$ ./a.out
t1 = 1530119936
t2 = 1521727232
hello, world
hello, world
hello, world
, ", " 3 . -, , , 3 ?