How to realise barrier synchronization between threads?

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
rweickelt
Member++
Posts: 16
Joined: Tue Sep 02, 2014 8:38 pm

How to realise barrier synchronization between threads?

Post by rweickelt »

Hi,

I've started a set of threads with the par{} statement. How can I achieve a synchronisation barrier between those threads? The architecture manual mentions msync and ssync instructions, but I was hoping to find a more high-level solution.

Thanks
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

Just use channels :-)
User avatar
rweickelt
Member++
Posts: 16
Joined: Tue Sep 02, 2014 8:38 pm

Post by rweickelt »

Thank You for taking the time to answer. The concept of channels is still very new to me and I have problems to get my head around this. May I ask for a small example, e.g. with 3 threads? Or a hint to existing code that involves more than 2 threads?

I am also looking for an easy-to-use solution, that would not be bound to XC and hide the usage of XCORE primitives. The following example reflects what I had in mind.

Code: Select all

// main.xc
int main()
{
    par {
        thread0();
        thread1();
        thread2();
    }

    return 0;
}


// threads.h
extern "C" {
    void thread0();
    void thread1();
    void thread2();
}


// threads.cpp
Barrier<3> barrier;

void thread0()
{
    // do something
    barrier.signalAndWait();
    // do some more
    barrier.signalAndWait();
    // ...
}

void thread1()
{
    // do something
    barrier.signalAndWait();
    // do some more
    barrier.signalAndWait();
    // ...
}

void thread2()
{
    // do something
    barrier.signalAndWait();
    // do some more
    barrier.signalAndWait();
    // ...
}

 
Barrier could be a template class, but still, I have no idea, how to realize the low-level backend.
User avatar
rweickelt
Member++
Posts: 16
Joined: Tue Sep 02, 2014 8:38 pm

Post by rweickelt »

Thanks to this thread pointing to this code I was able to replace the par construct with a thread group and got a working synchronization barrier.