0

I am trying to tell one thread that it's ok to continue with calculations after the other thread reads data from a named pipe. Currently, I have a while infinite loop which checks everytime for new data. But this is inefficient as it consumes 100% of the CPU...

My question is... Is there any way to wait until one thread has checked for new data and tell the other thread that it's ok to continue (something like a signal)?

Luis Cruz
  • 1,488
  • 3
  • 22
  • 50
  • 7
    Your problem is exactly what a [condition variable](http://en.cppreference.com/w/cpp/thread/condition_variable) is built to solve. – huu Apr 28 '15 at 05:46
  • In this situation make a sense to use mutex. Check this [link](https://stackoverflow.com/questions/4989451/mutex-example-tutorial) – VirtualSnake Apr 28 '15 at 06:26

2 Answers2

0

There are various solutions for your problem:

  • Signals and slots model of Qt
  • C++: Boost.Signals2 or Boost.Signals (deprecated)
  • C++: libsigc++ - template-based
  • C++: sigslot
  • C++: XLObject - template-based, Qt-oriented.
  • C++: Signals
  • C: libevent - multi-threaded, platformindependent
  • C: libev - minimal alternative to libevent.

Have a look at them and find the one that fits the best for you.

erikzenker
  • 752
  • 5
  • 18
-2

Qt has everything you want, QThread works good with signals and slots.

klatoverato
  • 35
  • 2
  • 5
  • 1
    The C++11 Standard Library also has everything needed, and would generally be the first option considered, but given the question implies existing threaded code - whatever library's already in use can probably be used for this too.... – Tony Delroy Apr 28 '15 at 06:22
  • Qt is useable in his question, he asked and I answered, what the problem? – klatoverato Apr 29 '15 at 02:39