我想用pthreads編寫一個C++函數來進行排序。接受以下2個錯誤,不知道爲什麼:C++不完整類型和轉換錯誤
struct threadData{
int thread_id;
int stopIndex;
int startIndex;
};
void createThreads(int k){
struct thread_data threadData;
int numThreads = k;
int i = 0;
int err = 0;
pthread_t *threads = static_cast<pthread_t*>(malloc(sizeof(pthread_t) * numThreads));
for(i = 0;i<numThreads;i++){
threadData[i].thread_id = i;
//start and stop are 1 indexed
if(i==0){
threadData[i].start = ((N/k)*i)+1;
}
else{
threadData[i].start = ((N/k)*i);
}
threadData[i].stop = ((N/k)* (i+1));
err = pthread_create(&threads[i], NULL, bubbleSort, (void *)&threadData[i]); // replace foo with bubbleSort()
if(err != 0){
printf("error creating thread\n");
}
}
}
void *bubbleSort(void *threadArg){
struct threadData *threadData;
threadData = (struct thread_data *) threadArg;
printf("debug\n");
bool sorted = 0;
int x;
while(!sorted){
int start = threadData->startIndex;
int stop = threadData->stopIndex;
sorted = 1;
for(x = num_array[start]; x < num_array[stop-1]; x++){
if(num_array[x] > num_array[x+1]){
int temp = num_array[x+1];
num_array[x+1] = num_array[x];
num_array[x] = temp;
sorted = 0;
}
}
if(sorted){
break;
}
sorted = 1;
for(x = stop; x > start+1; x--){
if(num_array[x-1] > num_array[x]){
int temp = num_array[x-1];
num_array[x-1] = num_array[x];
num_array[x] = temp;
sorted = 0;
}
}
}
}
我收到的錯誤是: cse451.cpp:在功能 '無效createThreads(INT)': cse451.cpp:99:21:錯誤:骨料'createThreads(INT):: thread_data threadData具有不完整的類型和不能被定義 cse451.cpp:在函數 'void *冒泡(無效*)': cse451.cpp:127:38:錯誤:不能轉換「冒泡(void *):: thread_data *'到'threadData *'在作業中
我需要threadData來包含startIndex和stopIndex,它指向數組的邊界ch線程應該排序。看起來好像我的結構實現可能不正確,但我不確定爲什麼不。所有的幫助表示讚賞。
什麼是thread_data,它在哪裏定義? – P0W 2015-03-13 20:02:42