I create a few threads in parallel performing the following thread function, the input parameter to the thread functions is a integer. different threads get different integers. In the thread function, I define a local variable thread_index to accept this integer which will be used in the calculation. My problem is that these local variables will be messed up with those from other threads, which I think should not happen. In fact, there is a loop in each thread function and the first attempt of the loop is okay for all the threads but after that, the local variable 'thread_index' is messed up. I used semaphore barrier to protect the calculation process, but that should not be my current problem. All the variables not declared are pre-defined in the main function. I could not figure out why the local variables got interrupted by others.
The following is the thread creation process, where N is an integer to control the number of threads, pthread_t t[N/2]. for(long thread_index = 0;thread_index < N/2; ++thread_index){ pthread_t t[N/2]; pthread_create(&t[thread_index],NULL,exchange_swap,(void *) thread_index);
void *exchange_swap(void *ptharg){
// cout<<"num_phases "<<num_phases<<endl;
long thread_index = (long) ptharg;
for (phase = 1; phase <= num_phases;)
{
int num_groups = pow(2, (phase-1) % num_stages);
// cout<<num_groups<<endl;
int group_size = N / num_groups;
int gindex = (thread_index) / (group_size / 2);
int mindex = (thread_index) % (group_size / 2);
int group_start = gindex * group_size;
int group_end = (gindex + 1) * group_size - 1;
int data1 = group_start + mindex;
int data2 = group_end - mindex;
// swap
int temp;
if (sortArray[data1]>sortArray[data2])
{
temp = sortArray[data1];
sortArray[data1] = sortArray[data2];
sortArray[data2] = temp;
}
sem_wait(&s);
cout<<"Thread "<<thread_index+1<<" finished stage "<<(phase - 1) / num_stages + 1<<" phase "<<(phase-1) % num_stages + 1<<endl;
finished_job++;
if (finished_job == N/2){
finished_job = 0;
phase++;
for (int i = 0; i < N; ++i)
{
cout<<sortArray[i]<<" ";
}
cout<<endl;
sem_post(&barrier);
}
sem_post(&s);
sem_wait(&barrier);
sem_post(&barrier);
}
return NULL;
}
long thread_index = (long) ptharg;and notlong thread_index = *(long*) ptharg;? - phonetaggerstd::thread(I'm guessing here, but it seems like you don't use it). - Some programmer dude