Payne
Payne

Reputation: 476

Parallel threads synchronization

I have an question.

I'm trying to create some thread synchonization with main thread, using C++, but better if you answer via pseudocode. I doesn't use any frameworks for C++ and don't want to use them!

Here is my problem:

I have an main thread that run some code in loop, and threads (don't know how much before, but I can save them in some list when needed) that run other code in loops. Code of threads (each other) can be same or different, doesn't metter. Here is some basic scheme scheme

All threads that are runned should met on S1. Then should do their jobs (Run something) and wait each other on S2 point. I need help with synchronization structures, that I should use for sync threads.

My plan was have 2 FIFO - new and runnable threads.. When I create thread, it's added in new threads FIFO.. Somewhere before S1 point Main thread copy all threads from new to runnable threads, then set Barriere on number of threads in runnable, start all runnable threads, and on S2 should wait on Barriere. Then runlater queue and wait on S1 point. But I had problems with implementation of S1 synchronization point. My attemts was ending with dead locks.

Can you help me with some pseudocode for Main thread and Other threads? How to synchronize them in some proper way?

EDIT:

Pseudocode:

Main                       T1                          T2

While 1                    While 1                     While 1
  -- S1 --                   -- S1 --                    -- S1 --
  print a                    print b                     print c
  print d                    - blocked -                 print e
  - blocked -                - blocked -                 print f
  -- S2 --                   -- S2 --                    -- S2 --
  print ---               - blocked -                 - blocked -
End-While                  End-While                   End-While

Output should be something like:

a
d
b
c
e
f
---
b
a
d
c
e
f
---

Always block of a-f letters..

Need something to make blocking like in pseudocode.. Can't be more specific, don't know what exact I'm looking for..

EDIT 2: Becouse of lack of answer that I was looking for, I post some edited code in C++ like syntax that I'm using but dont works, maybe someone helps.

main() {
    Barrier s1;
    Barrier s2;

    s1.reset(2);
    initThread();

    while(1) {
        s2.reset(2);
        s1.wait();
        s1.reset(2);
        runSomething();
        s2.wait();
        runQueue();
    }
}

thread() {
    while(1) {
        s1.wait();
        run();
        s2.wait();
    }
}

I'm thinking about using of async, but better than rewrite code (without knowing the outcome) I would like to deal with this problem this way.

Upvotes: 1

Views: 1168

Answers (3)

Revolver_Ocelot
Revolver_Ocelot

Reputation: 8785

If you have latest compiler which support exprerimental features, barrier might be a good choice.

Upvotes: -1

Ryan
Ryan

Reputation: 313

I would use something like WaitForMultipleObjects(...) --> https://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx

The basic idea is this:

-Have your main thread run until it hits S1. Then have it sleep with WaitForMultipleObjects(...).

-Have the other threads run until they hit S1, and raise an event with SetEvent(..). Then have each sleep using WaitForSingleObject(..).

-Once each other thread has called SetEvent(..) , your main thread will wake up. Once it wakes up, it will call SetEvent(..) to wake up each other thread that is waiting using WaitForSingleObject(..). Now they'll all be executing between S1 and S2 in sync.

-Then do the exact same thing to synchronize when you reach S2.

You don't necesarrily have to use that framework, but that's the general design for thread synchronization.

Upvotes: 1

kirinthos
kirinthos

Reputation: 452

This question seems a little too vague...nonetheless, there are quite a few ways to synchronize threads. You could use mutexes, condition variables, and atomics

ISOCPP published a PDF on concurrency that's pretty thorough.

Upvotes: 1

Related Questions