c - Is this a printf()/pthread bug, or am I missing something? -
c - Is this a printf()/pthread bug, or am I missing something? -
i endeavoured larn multiple threading, , ran next unexpected - me, @ to the lowest degree - behaviour: printf not print more line @ 1 time when called in simple next code:
pthread_mutex_t mutex = pthread_mutex_initializer; pthread_cond_t cond = pthread_cond_initializer; char buffer[2]; void * thread_routine(void * args){ pthread_mutex_lock(&mutex); pthread_cond_wait(&cond, &mutex); printf("test %s\n test\n", buffer); pthread_mutex_unlock(&mutex); return(null); } int main(void){ pthread_t thread; pthread_create(&thread, null, thread_routine, null); sleep(1); buffer[0] = 'c'; buffer[1] = '\0'; pthread_mutex_lock(&mutex); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); sleep(10); return(0); }
the output
test c
(wait 10 seconds and)
test prompt$]
what wrong code? how come can't printf print 2 lines @ once? please note blocking stdout flockfile , unlocking funlockfile nil improve situation.
if programme printed test prompt$]
@ end say, means version executed didn't have sec newline in "test %s\n test\n"
.
newlines important, since when output gets flushed screen. see why printf not flush after phone call unless newline in format string? (in c) explanation , recommendations.
try re-compiling , re-running exact code question, , bet it'll work expected (it on box).
c pthreads printf mutex
Comments
Post a Comment