If you have n threads, create an array of n binary semaphores (also known as locks or non-recursive mutexes). Semaphore [] locks = new Semaphore [n];
Before launching the threads, initialize all the locks to 0.
Every thread routine should get its number so that thread 0 will have a local variable indicating that it is thread 0.
Now every thread routine should start with locks[¡].lock(); and end with locks[(i+1)%n].unlock();
Then launch all the threads. And call locks[0].unlock()
The general idea or recipe here is thread synchronization. You start with locks initialized to lock, and then unlock them in the order you want to create a sequence. That's in contrast to thread protection where you typically start with locks in an unlocked state, and lock them when you are in a critical section.
Back to your problem, if you want the threads to go round and round. You can do this:
(java)
void run()
{
while(isRunning)
{
lock[i].lock();
////Your task
lock[(i+1)%n].unlock();
}
}
So for example thread 0 will get to the lock section and block until the main thread finished launching all the threads, and when the main thread calls locks[0].unlock(), thread 0 will perform the task, unlock thread and go back an get locked on lock[0] because it did not unlock it, thread 0 will wait for the last thread which will unlock it in the end of its routine.
EDIT:
You can have a similar solution with one mutex and n conditional variables, but the idea is the same.