Waking up the main thread

I am trying to write a GUI application for online system monitoring. I want to put the code that actually listens to the system on separate threads since they make blocking calls to an underlying library. When these calls return they should place their notifications on an event queue which the main thread (which handles the GUI) can read from and update the GUI.
Now the problem is that I need to wake the main thread up after something is added to the event queue, but it is not clear what the standard way to do this in ROOT is. The main thread would be busy in the TApplication->Run() method either dispatching events or sleeping while it waits for activity on the GUI. So how can I wake it up when it is asleep?


Well, I’m not sure I understand exactly what you mean by waking up main thread…
Did you have a look at thread examples in $(ROOTSYS)/tutorials and $(ROOTSYS)/test ?
I would suggest you to start from there and then post a more concrete question when you encounter a specific problem.
Or if you already have a piece of working code showing the problem, please post it, in order to save our (and your) time :wink:


OK, I added an example.
In the code in the threadexample.cxx file you will find some comments indicating the point at which I need to wake up the main thread (tell it that there is something on the event queue) so it can call UpdateCounter which will read from the event queue and update the GUI.

Side Note:

I notice some strange behaviour with the program. Sometimes when it runs the top bar with the minimisation icons is missing. I can not seem to reproduce this problem. But it does seem to go away when I comment out everuthing to do with threads.
thread_example.tar.gz (2.27 KB)


Here is your example, with a few modifications, and a small bug fix (double declaration of outputlabel). It is a very simple solution, but it works (at least on Windows). Please let me know if it solves your problem.

thread_example2.tar.gz (2.3 KB)

Thanks for catching the double declaration.
As for the added line: gui->UpdateCounter();
Although this may work on a single CPU machine, it does not look thread safe. Plus, I still end up having the child thread performing GUI updates which I want to avoid. The whole point of the event queue is to provide a channel of communication so that the child thread does not enter GUI code at all.
Normally if I was to code on the system level I would use signals or a custom event loop (the app.Run() part) which contains a pthread_cond_wait somewhere inside. But I dont know how compatible that is with ROOT nor how to customise or integrate the event loop that deals with GUI events. Thats why I asked what the typical way to do these things in ROOT is.

So, after digging in the ROOT code I see that system signals (at least the basic ones) are well integrated. I have decided to follow that route. But I can not find the equivalent of a system call like:

kill(getpid(), SIGUSR1);

Is there a ROOT equivalent? I want the code as portable as possible.
thread_example.tar.gz (2.44 KB)