What is the proper way of doing event handling in C++?

前端 未结 4 602
借酒劲吻你
借酒劲吻你 2020-11-30 06:40

I have an application that needs to respond to certain events in the following manner:

void someMethodWithinSomeClass() {
    while (true) {
        wait for         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 07:27

    C++11 and Boost have condition variables. They are a means for a thread to unblock another one that is waiting for some event to occur. The link above brings you to the documentation for std::condition_variable, and has a code sample that shows how to use it.

    If you need to keep track of events (say, keystrokes) and need to process them in a FIFO (first-in first-out) manner, then you'll have to use or make some kind of multi-threaded event queuing system, as suggested in some of the other answers. Condition variables can be used as building blocks to write your own producer/consumer queue, if you choose not to use an existing implementation.

提交回复
热议问题