The program below keeps outputting inconsistently both in the order of output and the resulting values. I am using the join functions and mutexes, but doesn't seem to work. Can't someone please explain to me why this may be the case?
The five threads will increment the x in any order, that's the whole point of running multiple threads at the same time.
Here for example one of my runs:
$ ./test
hello from thr_func, thread id: 0
hello from thr_func, thread id: 1
hello from thr_func, thread id: 4
hello from thr_func, thread id: 2
x = 5.000000
hello from thr_func, thread id: 3
x = 25.000000
x = 35.000000
x = 60.000000
x = 75.000000
as you can see, threads reached the first printf() in order 0,1,4,2,3 and then they grabbed the mutex in order 0,3,1,4,2. On other runs, the ordering was different, as expected.