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
How to realise barrier synchronization between threads?
-
- Member++
- Posts: 16
- Joined: Tue Sep 02, 2014 8:38 pm
-
- XCore Expert
- Posts: 844
- Joined: Sun Jul 11, 2010 1:31 am
Just use channels :-)
-
- Member++
- Posts: 16
- Joined: Tue Sep 02, 2014 8:38 pm
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.
Barrier could be a template class, but still, I have no idea, how to realize the low-level backend.
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();
// ...
}
-
- Member++
- Posts: 16
- Joined: Tue Sep 02, 2014 8:38 pm
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.