Reputation: 177
I'd like to know what is the best way to build a multi-threaded event system using Boost::signals2 and Boost::thread.
In my project, I have a network class that listen to incoming connection in a thread. When a player connects, the class should be able to raise an event to my server handler class and other functions listening to the signal. Thing is, the server and the other functions can be in threads too, and the called function MUST NOT be withing the network class context, but within the context that subscribed to the event.
What is the best way to do this? I have heard of polling but I'm not sure how this can be used here, using threads and signals.
tldr: Be able to listen to signals that are in other threads.
Upvotes: 0
Views: 908
Reputation: 9573
Since you're writing network-based code in anyway, have you considered boost asio?
To elaborate briefly, calling boost::asio::io_service::run from multiple threads allows you to handle events in a multi-threaded manner. Further, you can post work to the io_service using io_service::post, which will cause the work to get executed by the io_service in the threads that are blocked on the run method. For more info, read this SO post. The game-dev link provides a nice introduction to asio and to how it can be used. asio also provides a poll method, and much much more.
To me it sounds like asio is a nice fit for what you are trying to achieve.
Upvotes: 2
Reputation: 1772
As far as I understand, boost signals2 doesn't give you what you want. It's thread safe, but it can't execute code on another thread for you.
It sounds to me like you should put events into a queue rather than trying to use signals. Your consumer thread can simply block until an event is available in the queue.
Is there a requirement that you must use signals as part of your solution?
Upvotes: 2