use pthread_create to create limited number of threads running concurrently Successfully compile and run
However, after adding function pointer array to run the function, Segmentation fault
Where is wrong?
workserver number: 0 Segmentation fault
void* workserver(void *arg)
{
int status;
while(true)
{
printf("workserver number: %d\n", (int)arg);
(* job_queue[(int)arg])();
sleep(3);
status = pthread_mutex_lock(&data.mutex);
if(status != 0)
printf("%d lock mutex", status);
data.value = 1;
status = pthread_cond_signal(&data.cond);
if(status != 0)
printf("%d signal condition", status);
status = pthread_mutex_unlock(&data.mutex);
if(status != 0)
printf("%d unlock mutex", status);
}
}
void jobadd(void (*job)())
{
for(int i; i<3; i++)
{
if(idle[i] == 0)
{
job_queue[i] = job;
job;
idle[i] = 1;
}
}
}
void func1()
{
printf("func1 run");
}
void func2()
{
printf("func2 run");
}
void func3()
{
printf("func3 run");
}
void func4()
{
printf("func4 run");
}
int main(int argc, char* argv[])
{
jobadd(func1);
jobadd(func2);
jobadd(func3);
jobrun();
return 0;
}
initialize function pointer array with jobadd function