Home Help Center

c++ threading example docs

skroppskropp Member Sebastian KroppOrganization: MicrolutionProject: Microlution Logger
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?
cheers
--
Sebastian Kropp
Masters in IT (Melbourne)
Software Engineer

Comments

  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    No, that cannot happen: each writer enqueues exactly 100 items, and each reader dequeues exactly 100 items. That guarantees that all readers will finish.

    Cheers,

    Michi.
  • skroppskropp Member Sebastian KroppOrganization: MicrolutionProject: Microlution Logger
    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.
  • matthewmatthew NL, CanadaMember Matthew NewhookOrganization: ZeroC, Inc.Project: Internet Communications Engine ✭✭✭
    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.
Sign In or Register to comment.