Question
Write a program that starts two detached threads that communicate via a
global string variable.
- The main thread must
- Set the common global string to empty
- Start the communicating threads
- Detach the threads
- Exit
- The producer thread must run a loop that:
- Yields the CPU until the global string is empty
- Prompts the user for a string
- Reads it into the global string
until the user enters ^D (end of file). At that point the producer
can cause the entire process to terminate.
- The consumer thread must run an infinite loop that:
- Yields the CPU until the global string is non-empty
- Prints the global string to the screen
- Sets the global string to empty
Answer