You are relying on threads started running immediately or something like that? There are definitely better ways to do this.
Your threads should wait on their semaphore before doing anything else.
I'd move the scheduling logic to one common spot (maybe pass in thread type, and number of iterations, and it sends out signals).
I'd have each sem_post
signal a single loop iteration request. So if you want C
to run 3 times, call sem_post
3 times.
I have no idea what you are doing with the first argument to pthread_create
. Overwriting a function with thread data? Bad idea.
As this is C++, I'd wrap up the creation of threads into an object. And I'd pass in things like the semaphore to wait on in the void*
arg.
I suspect you either need more experience writing multi-threaded code, or the ability to debug on a live platform, in order to succeed at your task.