1

作業項目のキューを処理するワーカー スレッドがあります。

//producer
void push_into_queue(char *item) {
    pthread_mutex_lock (&queueMutex);
    if(workQueue.full) { // full }
    else{
        add_item_into_queue(item);
        pthread_cond_signal (&queueSignalPush);
    }   
    pthread_mutex_unlock(&queueMutex);
}
// consumer1
void* worker(void* arg) {
    while(true) {
        pthread_mutex_lock(&queueMutex);
        while(workQueue.empty)
            pthread_cond_wait(&queueSignalPush, &queueMutex);

        item = workQueue.front; // pop from queue
        add_item_into_list(item);

        // do I need another signal here for thread2?
        pthread_cond_signal(&queueSignalPop);
        pthread_mutex_unlock(&queueMutex);
    }   
    return NULL;
}
pthread_create (&thread1, NULL, (void *) &worker, NULL);

thread2ここで、挿入されたデータを消費したいと思いますadd_item_into_list()が、アイテムがリストに追加された場合に限ります。リストは永続的であり、プログラムの全期間中、空にしたり解放したりできないことに注意してください。

だから私の質問は: 私は別のものを必要としpthread_cond_signalますか? はいの場合、この信号はどこに行きますか? そして私の他の労働者はどのように見えるでしょうか(正規形)?

4

1 に答える 1