[Thread] finding a good expression to wait...
Hello everyone! I'm trying to build a simple user-interface. It reads events from the keyboard using cin in one thread and then yields to a controller, which does some work. I'm doing the waiting part in the threads using condition_variables. The input-threads "main-function" looks something like this: *** CODE SNIPPET *** condition input_cond, output_cond; // some other code void main_loop(condition& in, condition& out) { char input_key; boost::mutex::scoped_lock lk(my_global_mutex); while (input_key != 'q') { while (in.timed_wait(lk,system_time(second_clock::local() + seconds(1)) \ == false) // DEFINITELY NOT! good { } cin >> input_key; out.notify_one(); } } *** END OF CODE *** The controller/output main_loop looks similar only it uses out.timed_wait(...) and in.notify_one(), when the crucial task is done. Result is, both main_loops, when passed to a thread start and then it's deadlocked. I have seen examples of the producer/consumer pattern, which worked with buffers and they used conditions like: while (buffer_full == 0) or similar. I only have one key, no queueing, so what to do here? Please any good pointer/help/example is appreciated. Gratefully yours Julien -------- Music was my first love and it will be my last (John Miles) ======== FIND MY WEB-PROJECT AT: ======== http://ltsb.sourceforge.net the Linux TextBased Studio guide ======= AND MY PERSONAL PAGES AT: ======= http://www.juliencoder.de
AMDG Julien Claassen wrote:
Hello everyone! I'm trying to build a simple user-interface. It reads events from the keyboard using cin in one thread and then yields to a controller, which does some work. I'm doing the waiting part in the threads using condition_variables. The input-threads "main-function" looks something like this:
*** CODE SNIPPET *** condition input_cond, output_cond; // some other code void main_loop(condition& in, condition& out) { char input_key; boost::mutex::scoped_lock lk(my_global_mutex); while (input_key != 'q') { while (in.timed_wait(lk,system_time(second_clock::local() + seconds(1)) \ == false) // DEFINITELY NOT! good { } cin >> input_key; out.notify_one(); } } *** END OF CODE *** The controller/output main_loop looks similar only it uses out.timed_wait(...) and in.notify_one(), when the crucial task is done. Result is, both main_loops, when passed to a thread start and then it's deadlocked. I have seen examples of the producer/consumer pattern, which worked with buffers and they used conditions like: while (buffer_full == 0) or similar. I only have one key, no queueing, so what to do here? Please any good pointer/help/example is appreciated.
You effectively need a blocking queue that holds at most one element.
#include
Hello! In Sep 7 A.D. 2009 Steven Watanabe scripsit:
AMDG
Julien Claassen wrote [...]
I have seen examples of the producer/consumer pattern, which worked with buffers and they used conditions like: while (buffer_full == 0) or similar. I only have one key, no queueing, so what should I do here?
You effectively need a blocking queue that holds at most one element.
#include
#include #include #include #include <iostream> using boost::lambda::var;
boost::condition in, out; boost::mutex m; char input_key; int size;
void reader() { boost::unique_lockboost::mutex l(m); while(input_key != 'q') { in.wait(l, var(size) < 1); std::cin >> input_key; size = 1; out.notify_one(); } }
Thank you, Steven, for your solution. But now I have a problem. I used boost::lambda::var both in my read and control/output function, both still member functions of their respective classes. So the boost::lambda::var in the controller/output thread has a real int, while the boost::lambda::var in the input function has a reference to that same int. Not sure if this is the problem. Now working with the Boost library collect 1.4.0 and g++ (Debian 4.3.2-1.1) 4.3.2 Compilation for a small example program takes very long (over 10-15 seconds!) and then produces a very long error. The gist of it, as I understand is: depth for template nesting is exceeded. There should be a depth > 500 somewhere and it is all about Boost.Lambda. Would someone have an idea on that? May it have to do with the class-structure and the reference passed to the boost::lambda::var? Kindest regards Julien -------- Music was my first love and it will be my last (John Miles) ======== FIND MY WEB-PROJECT AT: ======== http://ltsb.sourceforge.net the Linux TextBased Studio guide ======= AND MY PERSONAL PAGES AT: ======= http://www.juliencoder.de
AMDG Julien Claassen wrote:
Thank you, Steven, for your solution. But now I have a problem. I used boost::lambda::var both in my read and control/output function, both still member functions of their respective classes. So the boost::lambda::var in the controller/output thread has a real int, while the boost::lambda::var in the input function has a reference to that same int. Not sure if this is the problem.
It shouldn't be a problem. Can you post the exact expressions? Of course, you can always use the traditional form of a wait loop: while(size == 0) condition.wait(lock);
Now working with the Boost library collect 1.4.0 and g++ (Debian 4.3.2-1.1) 4.3.2 Compilation for a small example program takes very long (over 10-15 seconds!) and then produces a very long error. The gist of it, as I understand is: depth for template nesting is exceeded. There should be a depth > 500 somewhere and it is all about Boost.Lambda. Would someone have an idea on that? May it have to do with the class-structure and the reference passed to the boost::lambda::var?
In Christ, Steven Watanabe
Hello! @Steven: I am using the traditional form of a while-loop now, instead of the boost::lambda::var. Thanks in any case for pointing all that out to me! Amiable yours Julien -------- Music was my first love and it will be my last (John Miles) ======== FIND MY WEB-PROJECT AT: ======== http://ltsb.sourceforge.net the Linux TextBased Studio guide ======= AND MY PERSONAL PAGES AT: ======= http://www.juliencoder.de
participants (2)
-
Julien Claassen
-
Steven Watanabe