Not so long ago, I was faced with a fact that programming with Xlib
in the presence of multiple threads and multiple windows is a thorny path. The problem I was faced with is that, that Xlib
(or window manager) is forcefully closes a connection to the server after so called "top level" window is closed.
The problem is easy to reproduce, you just need to create a window, then close it. Try to use use a Display
after the above steps. The fatal error will raise and program will exit. This is a strange behaviour of Xlib
.
The problem were faced in several SO
topics: How do I gracefully exit an X11 event loop?, How do you exit X11 program without Error. To summarize the discussions, It is noted somewhere in Xlib documentation that windows manager will close the connection.
Out of curiosity I checked XCB
library for such behaviour. The XCB
, compared to Xlib, behaved "normally" and I was able to create and destroy several windows with connection still in place.
The question is. How does XCB
library performs in the presence of multiple windows and threads, so it does not show the Xlib
faulty behaviour? Does the XCB
uses Xlib
under the hood, or does it sends commands to the X server in some other format? Excuse me, I am not so familiar, how to communicate to X11 protocol. And in particular, how it come, that window manager does not close the connection with XCB
?
I will accept any answer that will shred a light on the matters. Will it be web-links to XCB
source code, that explains the smart way of doing things. Or will it be explanation in terms of X11
protocol. Or anything else.
Thanks for the long reading. I hope I did not tire you with all this text.
Update
I've messed things up. The Xlib behaves as I expect, if WM_DELETE_WINDOW is set in WM_PROTOCOLS. It is just the ordering, or syncing, caused the connection to be in wrong state. I think that multiple threads have interfered with Xlib
state in a random way and the event loop got wrong. I must warn others about Xlib, that a syncing should be made by means of variables or atomic primitives. The Xlib
actual state change should be made only in the main thread and after the event loop had passed.