I am trying to create an array of semaphores, however my code is not running correctly so I am hoping to get some feedback on whether I am doing this correct.
Creating the semaphore array as a global variable, I have:
sem_t sems [10] = {};
Then filling the array (in main):
sem_t sem0;
sems[0] = sem0;
sem_t sem1;
sems[1] = sem1;
sem_t sem2;
sems[2] = sem2;
...
And finally, initalizing the semaphores:
sem_init(&sem0, 0, 1);
sem_init(&sem1, 0, 0);
sem_init(&sem2, 0, 0);
...
I am referring to each semaphore by using &sems[threadID], where each threadID is between 0 and 9. It seems to work, but sem_wait() is being ignored and threads are running when they should be waiting. I am not great at C++ and am hoping for any indication of what I am doing wrong. Thank you in advance.