0

I have a project that needs multiple thread and event generation in c++. I have 4 different sensors send data and a user interface that use TCP. They must run at the same time. I have a TCP class that has functions to check connection, send messages and get messages. Each these of function need to generate event(for example in checkConnection() function generate an event when connection fail or ok, in getMessages() function generate an event when message received etc.). I have 5 threads, 4 of them for sensor data and 1 of them for user interface. But i couldn't generate events in c++. How do i generate events and how do i use them with threads? Do you have any example about this?

  • 2
    What kind of events? Windows events? Qt events? Do you want to setup an own event system? – Scheff's Cat May 13 '20 at 11:12
  • I am using windows OS but i want to design platform-independent events. @Scheff – DikotaLolly May 13 '20 at 11:26
  • OK. So, you want to design your own event system. This is something worth to be [edit]ed in, isn't it? – Scheff's Cat May 13 '20 at 11:28
  • This is what I found with [google "c++ event system tutorial"](https://www.google.com/search?q=c%2B%2B+event+system+tutorial): [**CppEvent - How to Implement Events using Standard C++**](https://www.codeproject.com/Articles/1256352/CppEvent-How-to-Implement-Events-using-Standard-Cp) – Scheff's Cat May 13 '20 at 11:30
  • Your central facility has to be an event queue. (`std::deque` could be appropriate or `std::queue`.) To generate events, add entries to the end of queue. To process events, take them from front of queue (if queue is not empty). Processing of events could be done in a loop by registered function handlers or (even simpler) in a `switch`. Of course, with multi-threading, you have to add thread sync. So, your event queue should use e.g. a `std::mutex` to make concurrent accesses for sending and processing of events mutual exclusive. – Scheff's Cat May 13 '20 at 11:38
  • To prevent unecessary looping while waiting on an event in your main thread, a `std::condition_variable` could be used to make the event process function blocking (until an event is arrived). Example in my answer to [SO: C/C++: How to exit sleep() when an interrupt arrives?](https://stackoverflow.com/a/61496169/7478597) – Scheff's Cat May 13 '20 at 11:40
  • You probably should think twice before you go multithreaded. Maybe single threaded works, too. And it is much easier. – Werner Henze May 13 '20 at 12:17
  • Thank you @Scheff. These are very useful for me. – DikotaLolly May 17 '20 at 12:37

0 Answers0