This forum has been archived. Please start a new discussion on GitHub.

c++ threading example docs

i have some trouble understanding the monitor queue example. after 5 producer and consumer are started, you are joining for all threads to complete. but can it not be, that if all queue items have been consumed, that there are still threads hanging in get()? so main could never join?
Sebastian Kropp
Masters in IT (Melbourne)
Software Engineer


  • No, that cannot happen: each writer enqueues exactly 100 items, and each reader dequeues exactly 100 items. That guarantees that all readers will finish.


  • but how can you design it, if the number of items is not known in advance and you still want to finish those consumer threads. guess this scenario is very common.

    i am very unsure how to do this efficiently. right now i am pulsing all threads when the producer have finished. the awakened thread realizes that the producer have finished and throws an exception instead of returning an item.
    Problem with this is the added complexity of handling exceptions.

    with your experience, would you do the same or how would you approach such a problem.
  • matthew
    matthew NL, Canada
    You mean you want to stop the consumer thread when its time to terminate main? In this case you must have a destroy() function that sets a _destroy flag and notifies the waiting consumer. The consumer detects that the destroy flag is set, and terminates.