[Xcb] Thread safety of XCB.

Josh Triplett josh at joshtriplett.org
Tue Oct 2 20:58:45 PDT 2012


On Tue, Oct 02, 2012 at 11:43:42PM +0300, John Found wrote:
> I am trying to use XLib (based on XCB) in multithreaded application, where the threads are created by direct call to sys_clone.
> Unfortunately, when several threads calls XLib functions, the application crashes with following message:
> 
> >>>../../src/xcb_io.c:378: _XAllocID: Assertion `ret != inval_id' failed.
> 
> What is the reason for this crash? What XCB expects from the threads in order to be "thread safe"?
> 
> The clone flags I use are: CLONE_SIGHAND, CLONE_THREAD, CLONE_FILES, CLONE_FS, CLONE_VM, CLONE_PTRACE, CLONE_PARENT
> The created threads actually works and terminates as expected, with one exception - XCB (XLib) calls.
> 
> When I try the same, but using pthread library everything works fine, but I can't use pthread library in this application.
> It must be sys_call that to be used for thread creation.

First of all, since you use threads with Xlib, you must call
XInitThreads before any other call to Xlib, including XOpenDisplay.
Have you done so?

Second, even though you don't use pthreads, Xlib and XCB do so
internally; specifically, they use pthread locks and condition variables
for synchronization.  If your application does not link to libpthread,
those internal uses will go to the stub functions in glibc (or the stub
functions in libpthread-stubs on systems without stubs in their libc), which don't
actually do proper locking.  So, if you use threading with Xlib or XCB,
you must compile with -pthread, even if you don't use libpthread
yourself.  (Or, alternatively, you can call Xlib and XCB exclusively
from a single thread.)

- Josh Triplett


More information about the Xcb mailing list