C++/Threads

Advertisement


Question
QUESTION: How do I wake up a thread, so that thread 0 can wake up thread 1 because there are not enough 0's.

ANSWER: Hello Jwalit

Usually, a thread will be waiting on some resource and will wake up when the resource becomes available. For example, in posix threads you could have a thread waiting on a mutex or condition variable. When the mutex is available, or the condition variable is signalled, a waiting thread will wake up.

To better answer your question, I would need to know what your program is to accomplish, which threading library you are using, and why the thread was sleeping in the first place. Actually, for C and C++ I've only used posix pthreads and WIN32 threads so I couldn't really help you with other threading libraries.

Best regards
Zlatko


---------- FOLLOW-UP ----------

QUESTION: Hey my program is to accomplish synchronize 2 threads so they print out
at a bit sequence of 0's and 1's

The total number of 1's that have been printed at any point in the ouput
string cannot exceed the total number of 0's that have been printed at that point

A '1' cannot be printed until 2 or more '0''s have been printed.



Thanks

Answer
Hey is for horses. You can think of this as a producer/consumer problem. The fact that the total number of 1's cannot exceed the total number of 0's is like saying that you cannot consume more than has been produced. Of course nothing is really being produced here but as with a real producer / consumer, you need to keep a count of items available to be consumed. The count is incremented whenever the imaginary product is produced and decremented whenever it is consumed. Along with the production, a 0 is printed, and along with the consumption a 1 is printed.

In general you would use a condition variable to synchronize the producer and consumer. The consumer waits on the condition variable. The producer increments the count and signals the condition variable. The consumer then wakes up, looks at the count, acts on it and decrements it. The consumer then goes back to waiting on the condition variable.

Your last requirement is unclear.
"A '1' cannot be printed until 2 or more '0''s have been printed."

Does it mean that only when the program starts, there must be two 0 before the first 1, or does it mean that at any time during the program run, there could be a string of 1s but after the last 1 is printed, there must be two or more 0 before a 1 can be printed again?

For example, I guess this is invalid 0101.
This is valid 001.
Is this valid 0011?
Is this valid 0010101?

Along with these questions, you should tell me which threading library you are using. If you don't know, just tell me the function you use to create a thread. Better yet, show me an attempt at writing the code.

Good luck and best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.