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

Thread: How to synchronize a worker thread from the main thread?

  1. #1
    Join Date
    Dec 2010
    Posts
    907

    How to synchronize a worker thread from the main thread?

    I would just hand off some tasks like calculations to the worker thread(s), the main thread will continue on
    with its own work. The main thread will take as much outcome as it can from the worker thread,
    The main thread will never be locked out, but the worker thread will,
    whenever the main thread is interpolating the worker's output. (When the worker
    thread can't catch up, the main thread just stand still and never locked out)
    What is the best way to synchronize between these 2 threads.
    Note that I can't have them run in parallel in random manner,
    The main thread has not to be interfered with it is doing its interpolation.
    Any ideas?
    Thanks
    Jack

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to synchronize a worker thread from the main thread?

    What do you mean by that:
    the main thread just stand still and never locked out
    Isn't it a contradiction?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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

    Re: How to synchronize a worker thread from the main thread?

    Does the main thread have any sort of display or UI?

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    Re: How to synchronize a worker thread from the main thread?

    To VladimirF: The main thread will just keep on working on things like physics, IK and stuff.
    To Arjay: Yes, the main thread will still be displaying things plus physics and IK and the rest of other things.

    Sorry, shouldn't have used the word "stand still", just mean idling on that part of the system.
    Other parts would still be active.
    Last edited by lucky6969b; August 1st, 2015 at 12:13 AM.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: How to synchronize a worker thread from the main thread?

    Quote Originally Posted by lucky6969b View Post
    The main thread will take as much outcome as it can from the worker thread,
    The main thread will never be locked out, but the worker thread will,
    whenever the main thread is interpolating the worker's output. (When the worker
    thread can't catch up, the main thread just stand still and never locked out)
    What is the best way to synchronize between these 2 threads.
    Note that I can't have them run in parallel in random manner,
    The main thread has not to be interfered with it is doing its interpolation.
    Any ideas?
    I'm really just guessing here, because your terminology is very unusual, but I think you are looking for a triple buffering approach.

    - Keep 3 instances (A, B and C) of the computation result: A is used by the worker thread, B by the main thread, and C is used to transfer data between the two threads.
    - Whenever the worker thread has finished computing a result, it atomically swaps A and C.
    - Whenever the main thread is ready to process another result, it atomically swaps B and C.

    You'll need to add some logic for when the worker thread is slower than the main thread.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to synchronize a worker thread from the main thread?

    Quote Originally Posted by lucky6969b View Post
    To VladimirF: The main thread will just keep on working on things like physics, IK and stuff.
    To Arjay: Yes, the main thread will still be displaying things plus physics and IK and the rest of other things.

    Sorry, shouldn't have used the word "stand still", just mean idling on that part of the system.
    Other parts would still be active.
    your main thread should ideally ONLY be worrying about UI stuff.
    "working on things like physics" is what you should be delegating to the worker thread (or sequence of worker threads).
    You synchronise by having the worker threads post a "I'm done with the work" back to the main thread (or originating thread).

    If your main thread produces work faster than the worker thread can handle, the worker thread should just ignore/drop some of the incoming jobs if this is feasible, or launch additional worker threads to keep up.
    the main thread will just idle, waiting for input and waiting for the "I'm done" messages from the worker threads, at whjch point it displays the results (which should be as lightweight as possible).

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)