multithreading - Xlib fails (Segmentation fault) even with each connection per thread -
so far know x11 xlib, multi threading programmer has 2 choices
- call enough xinitthreads
- or use new connection (xopendisplay) per thread.
suppose don't first first method xinitthreads() call. why second fail?
#include <x11/xlib.h> #include <thread> void startbasicwin() { display *display; if ( (display=xopendisplay(null)) == null ) { fprintf( stderr, "cannot connect x server\n"); exit( -1 ); } xclosedisplay(display); } int main() { std::thread t3 = std::thread(startbasicwin); std::thread t4 = std::thread(startbasicwin); std::thread t5 = std::thread(startbasicwin); std::thread t6 = std::thread(startbasicwin); std::thread t7 = std::thread(startbasicwin); std::thread t8 = std::thread(startbasicwin); std::thread t9 = std::thread(startbasicwin); t3.join(); t4.join(); t5.join(); t6.join(); t7.join(); t8.join(); t9.join(); } compiled g++ -o xlib_multi xlib_multi.cpp -lx11 -std=c++11 -pthread -g
sometimes produces output:
segmentation fault or
no protocol specified cannot connect x server :0 can be, can't use xopendisplay() without thread-synchronization? once x11 connections created xlib, use xlib in multi-threaded environment without problems? such assumption correct?
or xlib buggy multi-threading anyway?
chances xopendisplay() uses global variable internally not thread-safe or shares data between displays. don't think it's wise call xopendisplay within thread; suggest opening displays sequentially first, start threads display pointer. or protect code section around xopendisplay (and xclosedisplay!) mutex.
either way, fact there separate xinitthreads() call makes assumption fine "after" xopendisplay() dangerous.
Comments
Post a Comment