pthreads - Xlib Muti-thread program only works under strace -
i'm writing muti-thread program using xlib, pthread , cairo. program create thread in order draw ten points after click event.
the problem is:
after program drew 3 points, got crashed , xlib complaint
x error of failed request: badrequest (invalid request code or no such operation) major opcode of failed request: 0 () serial number of failed request: 67 current serial number in output stream: 97
however, can work when i'm using strace "strace ./a.out".
here's code-clips:
void *draw_point(void *arg) { //paint random-postion point int = 0; int seed; double x ,y; srand(seed); cairo_set_source_rgba (cairo, 1, 0.2, 0.2, 0.6); for(i = 0; i< 10; i++) { x = rand() % 200; y = rand() % 200; if(candraw) { cairo_arc (cairo, x, y, 10.0, 0, 2*m_pi); cairo_fill (cairo); } hasdraw = true; sleep(1); } return null; } bool win_main(void) { int clickx = 0, clicky = 0; unsigned long valuemask; xevent event; valuemask = buttonpressmask | buttonreleasemask | buttonmotionmask | pointermotionmask; xselectinput(display, win, valuemask); pthread_t thread; while (1) { while (xpending(display) == 0) { candraw = true; if(hasdraw) xflush(display); candraw = false; } xnextevent(display, &event); candraw = false; switch (event.type) { case motionnotify: //... break; case buttonpress: clickx = event.xbutton.x; clicky = event.xbutton.y; if(clicky < 50) pthread_create(&thread, null, draw_point, null); break; case buttonrelease: //... break; default: break; } } return 0; }
does has idea weird problem? lot!
the problem caused using multi-threading 2 threads trying access display @ same.
strace change timing, threads accessing display @ different times.
xlib have functions prevent conflict. lookup xinitthreads, enables thread support , xlockdisplay , xunlockdisplay, need call each thread before , after accessing display.
Comments
Post a Comment