c++ - How to push message/event into a message queue from somewhere with no direct access? -


i have forever-run event loop in multithreaded program, lot of functions , methods push messages event loop. simplified version:

class eventloop { public:     void run(); private:     std::deque<std::string> m_msg_queue;     std::condition_variable m_q_cv;     std::mutex m_q_mtx; };  void eventloop::run() {      while(true) {          //use m_q_cv wait data in m_msg_queue , dispatch      } }  int main() {     eventloop el;     el.run();     return 0; } 

is approach correct? if so, what's best way enqueue events in other functions/methods other main()? don't doing extern event el in other .cpp files...

define contract posting or sending messages event loop , react received events. typical api expose @ least send/post pair of procedures plus kind of contract messages , mechanism hooking callbacks. expose contract instead of implementation might stay hidden inside of library.


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -