Почему моя функция продавца не выполняется, даже если поток создан успешно и ошибки компиляции нет? - PullRequest
0 голосов
/ 15 марта 2020
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;
}

1 Ответ

3 голосов
/ 15 марта 2020

Ваша программа завершает работу сразу после создания потока, потому что вы не ждете, пока поток завершит sh.

Добавьте это где-нибудь до того, как main() вернет:

void* retval;
int r = pthread_join(threads, &retval);

if(r==0) // successfully joined the thread threads
else     // failed to join the thread threads
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...