The code in question is:
void insertIntoFinalArray(char * string) {
sem_wait(&insert);
if (finalarray == NULL) finalarray = (char **) malloc(sizeof(char *));
else finalarray = (char **) realloc(finalarray, ((size_final + 1) * sizeof(char *)));
finalarray[size_final] = (char *) malloc(sizeof(string) + 1);
strcpy(finalarray[size_final], string);
size_final++;
sem_post(&insert);
}
insert is initialized as sem_init(&insert, 0, 0);
EDIT: this is supposed to be sem_init(&insert, 0, 1)
I was code stirring and just didn't set it back... it doesn't work either way.
As I understand it, the threads should wait at sem_wait() until the value is greater than zero. However, the debug shows multiple threads within the function.
So am I just not understanding what a semaphore does or ?
Edit: OS X doesn't support unnamed semaphores... However, even using insert = sem_open("insert", O_CREAT, 1);
allows all threads to get past my sem_wait() line.