c++ - pthread_mutex_lock blocks but __lock = 0 -
i writing multi-threaded program, , running deadlock.
one of threads blocks while other threads sleeping (cond_wait)
so entered ctrl+c go gdb terminal
(gdb) info thread 5 thread 0x1c6ff4a0 (lwp 723) __pthread_cond_wait (cond=0x420c50, mutex=0x420c34) @ pthread_cond_wait.c:156 3 thread 0x1bcf34a0 (lwp 721) __pthread_cond_wait (cond=0x41a530, mutex=0x41a514) @ pthread_cond_wait.c:156 * 1 thread 0x1b2c9720 (lwp 716) __lll_lock_wait (futex=0x1be08240, private=0) @ ../nptl/sysdeps/unix/sysv/linux/lowlevellock.c:46 (gdb) bt #0 __lll_lock_wait (futex=0x1be08240, private=0) @ ../nptl/sysdeps/unix/sysv/linux/lowlevellock.c:46 #1 0x1afa1540 in __pthread_mutex_lock (mutex=0x1be08240) @ pthread_mutex_lock.c:61 #2 0x1abb7984 in readerwait (frameinfo=0x1be08240, sem=0x1aad4000) @ camipcsource.cpp:282 ......
i can see thread blocked @ camipcsource.cpp:282
but when change frame #2 , print mutex
(gdb) frame 2 #2 0x1abb7984 in readerwait (frameinfo=0x1be08240, sem=0x1aad4000) @ camipcsource.cpp:282 (gdb) p frameinfo->m_reader $1 = {__data = {__lock = 0, __count = 0, __owner = 0, __kind = 0, __nusers = 0, { __spins = 0, __list = {__next = 0x0}}}, __size = '\000' <repeats 23 times>, __align = 0}
isn't weird? block on nonblock mutex?
and when let gdb continue, program lock
my question: else can know reason why thread block @ non-block mutex?
environment: compiled mipsel-linux-gnu-g++-4.4, running on mips (libpthread-2.11.3)
here code snippet(camipcsource.cpp) thread blocks:
277 int readerwait(frame_info_t* frameinfo, sem_t* sem) { 278 int ret; 279 280 if (frameinfo == null || sem == null) return -1; 281 282 pthread_mutex_lock(&frameinfo->m_reader); 283 /*while ((ret = pthread_mutex_trylock(&frameinfo->m_reader)) != 0) { 284 if (ret == ebusy) 285 fprintf(stderr, "-"); 286 else { 287 _err("pthread_mutex_trylock:(%s)\n", strerror(ret)); 288 return -1; 289 } 290 }*/ 291 292 frameinfo->readercount++; 293 if (frameinfo->readercount == 1) { 294 if (sem_wait(sem) != 0) { 295 _err("sem_wait:(%s)\n", strerror(errno)); 296 frameinfo->readercount--; 297 if ((ret = pthread_mutex_unlock(&frameinfo->m_reader)) != 0); 298 _err("pthread_mutex_unlock:(%s)\n", strerror(ret)); 299 return -1; 300 } 301 } 302 if ((ret = pthread_mutex_unlock(&frameinfo->m_reader)) != 0) 303 _err("pthread_mutex_unlock:(%s)\n", strerror(ret)); 304 305 return 0; 306 }
btw, i've tried replace pthread_mutex_lock pthread_mutex_trylock @ line 283~290
and works quite "-" output :(
here readersignal function (readerwait/readersignal 2 function access mutex)
339 int readersignal(frame_info_t* frameinfo, sem_t* sem) { 340 int ret; 341 342 if (frameinfo == null || sem == null) return -1; 343 344 pthread_mutex_lock(&frameinfo->m_reader); 345 /*while ((ret = pthread_mutex_trylock(&frameinfo->m_reader)) != 0) { 346 if (ret == ebusy) 347 fprintf(stderr, "-"); 348 else { 349 _err("pthread_mutex_trylock:(%s)\n", strerror(ret)); 350 return -1; 351 } 352 }*/ 353 frameinfo->readercount--; 354 if (frameinfo->readercount == 0) { 355 if (sem_post(sem) != 0) { 356 _err("sem_post:(%s)\n", strerror(errno)); 357 frameinfo->readercount++; 358 if ((ret = pthread_mutex_unlock(&frameinfo->m_reader)) != 0) 359 _err("pthread_mutex_unlock:(%s)\n", strerror(ret)); 360 return -1; 361 } 362 363 } 364 if ((ret = pthread_mutex_unlock(&frameinfo->m_reader)) != 0) 365 _err("pthread_mutex_unlock:(%s)\n", strerror(ret)); 366 367 return 0; 368 }
debugging multithreaded programs gdb typically difficult. better use tool designed handle multithreaded issues. highly recommend looking @ helgrind:
http://valgrind.org/docs/manual/hg-manual.html
from page: "helgrind valgrind tool detecting synchronisation errors in c, c++ , fortran programs use posix pthreads threading primitives."
it supply information on potential race conditions, deadlock, etc. pretty awesome really. has saved me more once.
good luck!
Comments
Post a Comment