My program contains the following code.
pthread_t PThreadTable[32];
for (i=1; i<P; i++) // Checked with P = 4
{
long i, Error;
printf( "pthread_create %d!\n", i );
Error = pthread_create(&PThreadTable[i], NULL,
(void * (*)(void *))(SlaveStart), NULL);
if (Error != 0)
{
printf("Error in pthread_create().\n");
exit(-1);
}
}
SlaveStart();
The code gives segmentation fault on calling pthread_create (checked through gdb and valgrind). Why so?
Its because you redeclare variable
iinside the loop. The variable inside the loop is being used and it contains garbage value. That is why, the expression&PThreadTable[i]points to a wrong address and you get a segmentation fault.