[問題] pthread 的 mutex 在什麼情況下會deadlock ?

看板C_and_CPP (C/C++)作者 (不可說不可說)時間16年前 (2009/12/01 06:22), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
(mandelbrot set) 在pthread+xlib programing下,由於xlib 不是thread safe,所以要用 mutex_lock包起來。 但是 mutex_lock 後來也好像卡住了? http://tinyurl.com/yhmxxos 沒卡住的時候就會有error message: X Error of failed request: BadRequest (invalid request code or no such operation) Major opcode of failed request: 0 () Serial number of failed request: 277465 Current serial number in output stream: 285661 把 xlib 相關的那兩行 mark掉就沒事了,所以問題應該是 mutex ? 我想問的是,mutex 不是那樣用嗎? 還是mutex要另外注意什麼? 還是 xlib+pthread 有其它的做法? 還是要用 conditional variable ? 我一直搞不懂 conditional variable 與 mutex 是差在哪裡 thanks platform: solaris + sun studio c 5.9 compiler (per thread code) for(int i = tid; i < X_RESN; i += num_threads) for(int j = 0; j < Y_RESN; j++) { c.real = ((float) j - MIN)/WIDTH; c.imag = ((float) i - MIN)/WIDTH; color = cal_pixel(c); color = color | color << 4 | color << 8; pthread_mutex_lock(&mutex); /* here */ XSetForeground(data->display, data->gc, color); XDrawPoint(data->display, data->win, data->gc, j, i); /*******/ pthread_mutex_unlock(&mutex); } XFlush(data->display); pthread_exit(NULL); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.203.27

12/01 06:30, , 1F
deadlock 印象中會被偵測出來,我在猜是不是 XFlush() 沒
12/01 06:30, 1F

12/01 06:30, , 2F
做 mutex,所以造成奇怪的 output。
12/01 06:30, 2F

12/01 08:04, , 3F
是啊,我怎麼會假設xflush是thread safe? 快去試一下
12/01 08:04, 3F

12/01 08:04, , 4F
thanks
12/01 08:04, 4F

12/01 08:12, , 5F
是xflush的問題。 many thanks
12/01 08:12, 5F
文章代碼(AID): #1B54MUlH (C_and_CPP)
文章代碼(AID): #1B54MUlH (C_and_CPP)