class Test { public: Test(); static std::condition_variable condVar; void notifyCv(); // тут делаем condVar.notify_all() } 

when trying to use such a variable, it returns undefined reference.

 void main() { std::mutex mut; std::unique_lock<std::mutex> lock(mut); while (true) { Test::condVar.wait(lock); //далаем чтото полезное } } 

The fact is that many instances of the Test class are created, and in these instances an event occurs that must be constantly waited. And you need to respond if an event occurred in any instance.

If you declare condVar, not as static, then each instance will have its own condVar and how then to expect an event from any of the instances without iterating over them all the time in a loop?

upd: yes, the question is similar to a duplicate .. that the static variable needs to be determined - I know. But I do not understand how to do this with condition_variable ..

  • As for the immediate error, it is a direct duplicate. As for the very idea of ​​using a static variable - this is a separate question ... - AnT February
  • @AnT The question you are asking is not about 'cond_var' .. Everything is clear with 'int'. Probably I had to write right away that I had a problem not with static itself, but with a lack of understanding of how to define static condition variables. So is it possible? - i6ecbl pm
  • And just above the main write something like std::condition_variable Test::condVar; - KoVadim

1 answer 1

The problem was solved with the help of the comment @KoVadim.

outside the class declaration add:

 std::condition_variable Test::condVar;