为什么线程创建成功,没有编译错误,我的seller函数却没有执行?
Why does my seller function does not get execute even though the thread is created successfully and there is no compilation error?
void* camera(void* arg)
{
int id = *(int *)arg;
cout << "please";
sleep(interval); // sleep interval seconds
int done = 0; /* 0 - not done; 1 - done */
long mysell = 0;
int s;
while(frame_cnt < 10){
if (rear == n - 1){// check if camera cache is full
sleep(interval);
}
else{
generate_frame_vector(8);
for (int i = 0; i < 8; i++){
rear++;
queue[rear] = frame_vector[i];
}
}
if(frame_cnt >= 10){
pthread_exit((void *) mysell);
}
}
}
int main(int argc, char* argv[]) { // Start
// Enter command, e.g: ./51234567 2 to run this program
if(argc == 2){ // if input is legal continue below with the rest of program execution
interval = atoi(argv[1]); // int interval stores the int value that will be used in sleep() later on
pthread_t threads;
int threadedid;
int rc; // rc is used to get the return value of pthread functions
rc = pthread_create(&threads, NULL, camera, (void *)&threadedid);
if (rc){
cout << "Error occured when creating the camera thread" << endl;
exit(-1);
}
}
else { // if input is not legal, then program will not fail to run and the message below displayed to the user
cerr << "Error occured because your argument is wrong; please use the format: './51234567 2' where 2 is the interval you want to use"
<< endl;
}
return 0;
}
您的程序在创建线程后立即终止,因为您没有等待线程完成。
在main()
之前的某处添加这个 returns:
void* retval;
int r = pthread_join(threads, &retval);
if(r==0) // successfully joined the thread threads
else // failed to join the thread threads
void* camera(void* arg)
{
int id = *(int *)arg;
cout << "please";
sleep(interval); // sleep interval seconds
int done = 0; /* 0 - not done; 1 - done */
long mysell = 0;
int s;
while(frame_cnt < 10){
if (rear == n - 1){// check if camera cache is full
sleep(interval);
}
else{
generate_frame_vector(8);
for (int i = 0; i < 8; i++){
rear++;
queue[rear] = frame_vector[i];
}
}
if(frame_cnt >= 10){
pthread_exit((void *) mysell);
}
}
}
int main(int argc, char* argv[]) { // Start
// Enter command, e.g: ./51234567 2 to run this program
if(argc == 2){ // if input is legal continue below with the rest of program execution
interval = atoi(argv[1]); // int interval stores the int value that will be used in sleep() later on
pthread_t threads;
int threadedid;
int rc; // rc is used to get the return value of pthread functions
rc = pthread_create(&threads, NULL, camera, (void *)&threadedid);
if (rc){
cout << "Error occured when creating the camera thread" << endl;
exit(-1);
}
}
else { // if input is not legal, then program will not fail to run and the message below displayed to the user
cerr << "Error occured because your argument is wrong; please use the format: './51234567 2' where 2 is the interval you want to use"
<< endl;
}
return 0;
}
您的程序在创建线程后立即终止,因为您没有等待线程完成。
在main()
之前的某处添加这个 returns:
void* retval;
int r = pthread_join(threads, &retval);
if(r==0) // successfully joined the thread threads
else // failed to join the thread threads