c++ - Using printf() changes multithreaded outcome? -
i'm writing multithreaded program run modified bubble sort class project. basically, each thread bubble sorts segment of array of integers, , each segment shares 1 element neighboring segments in order values flow between them. currently, using pthread_mutex_t
s around critical sections; must have wrong, because finishes sorted, finishes not sorted, , program hangs.
here's problem: if use printf()
s see in each thread, it's virtually guaranteed not hang (which makes difficult figure out it's getting hung up). why using printf()
s in sections of code run each thread seem prevent hanging , cause program finish?
edit: determined main cause of issue had not initialized mutexes (with pthread_mutex_init()
). chrk correct using improper synchronization, , printf()
usage slowed things down enough make things working.
i can't sure this, obviously, saying guess:
theoritically, printf(3)
uses system call write(2)
stdout
, i/o procedure, slower rest parts of code cpu calculations. if have other synchronization problem, time spent printf()
execute possibly accident "fix" instances of problem.
however that's not proper way fix synchronization problems, have check code again find race conditions being resolved in wrong way.
Comments
Post a Comment