CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Find n words given m threads

  1. #1
    Join Date
    Jul 2005

    Find n words given m threads

    I came across this question, You are given a paragraph , which contain n number of words, you are given m threads. What you need to do is , each thread should print one word and give the control to next thread, this way each thread will keep on printing one word , in case last thread come, it should invoke the first thread. Printing will repeat until all the words are printed in paragraph. Finally all threads should exit gracefully. What kind of synchronization will use?
    I really don't think we need to use multiple threads to print the words. In a single-threaded programming, we are able to use one thread to print out the words one by one. Is there any points I miss here? Thanks.

  2. #2
    Join Date
    Apr 2000
    Belgium (Europe)

    Re: Find n words given m threads

    The totality has no sense in this specific setting.
    It is little more than a simple example that demonstrates a simple handoff principle.
    One thread does some work, and then hands off the work it did to some other thread so the other thread can continue.

    There are many real world examples of this sort of approach.

    the task described is non-specific about what is "next thread".
    does it mean a thread just gives it to "any other" thread that is then the next in the chain of work.
    or does it mean thread 1 hands off to thread 2, which hands off to thread 3 in a very specific ordering.

    The first would be a case of normal blocking waits and whichever thread get through the block is decided by the OS.
    the 2nd would be each thread waiting for a specific signal from it's predecessor.

    as to implementation there are more than one ways to achieve either. One problem area is that threads wait for both a "we are all done" as well as "i have some work for you" signals.
    you can do that with a single event and a flag to indicate action to take
    or you can use multiple events.
    or you can use mutexes and events
    or ...

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004

    Re: Find n words given m threads

    Larry, you've been doing multi-threaded programming for a while - how would you approach this problem? Where would you start?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)