当我运行以下线程应用程序时,它大约25%的时间成功运行,其余部分通过并停止。似乎如果某些线程在一些线程已经启动之后创建它会停止正在运行的线程......但这只是猜测。它显然设法创建所有线程,甚至启动所有线程,但它不会在它停止之前从它们返回。是否有一些我缺少的线程管理?
struct parameters{
int *structArray;
int test;
vector<int> structVector;
};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* threadFunc(void* threadArg){
pthread_mutex_lock( &mutex );
int temp, number;
vector<int> tempVector(10);
struct parameters *local_data;
local_data = (struct parameters *) threadArg;
parameters *value = (parameters *)malloc(sizeof(parameters));
number = local_data->test;
cout << "In Thread Function: "<< number<< ": ";
for(int i = 0;i<10;i++){
temp = local_data->structVector[i];
cout << temp << " ";
tempVector[i] = number * (temp);
}
cout << endl;
value->structVector = tempVector;
value->test = 1000;
pthread_mutex_unlock( &mutex );
pthread_exit((void*)value);
}
int main(){
int numberOfThreads = 56;
pthread_t threads[numberOfThreads+1];
struct parameters ThreadParameters[numberOfThreads+1];
vector<int> mainVector(10);
int rc;
for(int i = 0;i<10;i++){
mainVector[i] = i;
}
for(int i = 0; i<numberOfThreads;i++){
ThreadParameters[i].test = i;
ThreadParameters[i].structVector = mainVector;
rc = pthread_create(&threads[i],NULL,threadFunc,(void*)&ThreadParameters[i]);
if(rc){
cout << "Error: unable to create thread, " << rc << endl;
return(-1);
}
else cout << "Thread number " << i << " created" << endl;
}
for(int i = 0; i<numberOfThreads; i++){
void *Results;
vector<int> receivedVector(10);
parameters answer;
pthread_join(threads[i], &Results);
answer = *((parameters *)Results);
free(Results);
receivedVector = answer.structVector;
cout << "\nReturn from thread " << i << ": ";
for(int j = 0; j<10;j++){
cout << receivedVector[j] << " ";
}
cout << endl;
}
cout << "\n\nTest complete";
}
以下是输出内容的示例:
Thread number 0 created
Thread number 1 created
Thread number 2 created
Thread number 3 created
Thread number 4 created
Thread number 5 created
Thread number 6 created
Thread number 7 created
Thread number 8 created
Thread number 9 created
Thread number 10 created
Thread number 11 created
Thread number 12 created
Thread number 13 created
Thread number 14 created
Thread number 15 created
Thread number 16 created
Thread number 17 created
Thread number 18 created
Thread number 19 created
Thread number 20 created
Thread number 21 created
Thread number 22 created
Thread number 23 created
Thread number 24 created
Thread number 25 created
Thread number 26 created
Thread number 27 created
Thread number 28 created
Thread number 29 created
Thread number 30 created
Thread number 31 created
Thread number 32 created
Thread number 33 created
Thread number 34 created
Thread number 35 created
Thread number 36 created
Thread number 37 created
Thread number 38 created
Thread number 39 created
Thread number 40 created
Thread number 41 created
Thread number 42 created
Thread number 43 created
Thread number 44 created
Thread number 45 created
Thread number 46 created
Thread number 47 created
Thread number 48 created
Thread number 49 created
Thread number 50 created
Thread number 51 created
Thread number 52 created
Thread number 53 created
Thread number 54 created
Thread number 55 created
In Thread Function: 5: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 7: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 10: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 12: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 52: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 20: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 24: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 26: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 29: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 37: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 43: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 45: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 22: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 14: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 41: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 18: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 27: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 33: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 39: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 47: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 48: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 50: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 54: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 16: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 35: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 3: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 2: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 1: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 0: 0 1 2 3 4 5 6 7 8 9
Return from thread 0: 0 0 0 0 0 0 0 0 0 0
Return from thread 1: 0 1 2 3 4 5 6 7 8 9
Return from thread 2: 0 2 4 6 8 10 12 14 16 18
Return from thread 3: 0 3 6 9 12 15 18 21 24 27
In Thread Function: 51: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 55: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 19: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 23: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 25: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 28: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 36: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 42: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 44: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 21: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 13: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 40: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 4: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 6: 0 1 2 3 4 5 6 7 8 9
Return from thread 4: 0 4 8 12 16 20 24 28 32 36
Return from thread 5: 0 5 10 15 20 25 30 35 40 45
Return from thread 6: 0 6 12 18 24 30 36 42 48 54
Return from thread 7: 0 7 14 21 28 35 42 49 56 63
In Thread Function: 8: 0 1 2 3 4 5 6 7 8 9
Return from thread 8: 0 8 16 24 32 40 48 56 64 72
Return from thread 9: 0 9 18 27 36 45 54 63 72 81
Return from thread 10: 0 10 20 30 40 50 60 70 80 90
In Thread Function: 11: 0 1 2 3 4 5 6 7 8 9
Return from thread 11: 0 11 22 33 44 55 66 77 88 99
Return from thread 12: 0 12 24 36 48 60 72 84 96 108
Return from thread 13: 0 13 26 39 52 65 78 91 104 117
Return from thread 14: 0 14 28 42 56 70 84 98 112 126
In Thread Function: 17: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 30: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 31: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 32: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 38: 0 1 2 3 4 5 6 7 8 9
In Thread Function: 46: 0 1 2 3 4 5 6 7 8 9