i have this really simple threading application downloaded off the net to test threading issues. its behaviour has changed since i switched to NPTL. i have recompiled glibc with +nptl -nptlonly take a look at this example and please tell me what you think about it and why the change occured (notice different char output): doli@1g ~/src $ ./thread o....................ooooooooooooooooooo myglobal equals 40 doli@1g ~/src $ export LD_ASSUME_KERNEL=2.4.1 doli@1g ~/src $ ./thread o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o. myglobal equals 40 here is the app: #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> int myglobal; pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; void *thread_function(void *arg) { int i,j; for ( i=0; i<20; i++ ) { pthread_mutex_lock(&mymutex); j=myglobal; j=j+1; printf("."); fflush(stdout); sleep(1); myglobal=j; pthread_mutex_unlock(&mymutex); } return NULL; } int main(void) { pthread_t mythread; int i; if ( pthread_create( &mythread, NULL, thread_function, NULL) ) { printf("error creating thread."); abort(); } for ( i=0; i<20; i++) { pthread_mutex_lock(&mymutex); myglobal=myglobal+1; pthread_mutex_unlock(&mymutex); printf("o"); fflush(stdout); sleep(1); } if ( pthread_join ( mythread, NULL ) ) { printf("error joining thread."); abort(); } printf("\nmyglobal equals %d\n",myglobal); exit(0); }
i dont see anything wrong with the differences, logically the code could produce either right ?
you are right, but what if i need this dummy pthreaded program to output exactly the pattern i need ? (o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.) i expect that if i mutex_lock() and then fflush() i would receive output above. i played with placing fflush() between mutex_lock() and mutex_unlock() with different effects which were all different from the original behaviour what would be the proper way of doing it ?
if you rely on threads to execute in a certain order and dont setup locks in order to achieve that, your code is broken already so in this case, the code is broken if you need it to output in that exact order
thanks for the comment and sorry for taking your time