0
以下のコードで助けてもらえますか?信号が私のスレッドワーカーに見えているとは思わない。私はSTEQUESIZE 1が印刷されることを期待しますが、決してそれは起こりません。どんな助けもありがとう。ワーカースレッドでのPthread状態信号
出力は次のようになります。
スレッド開始 ミューテックスコメントは私の質問に答えて行わ
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <pthread.h>
#include <errno.h>
// GLOBALS
pthread_mutex_t mutexqueue;
pthread_cond_t condworker;
pthread_t *threadIDs;
int STEQUESIZE=0;
void* worker(void *arg) {
fprintf(stdout,"Thread Started\n");
fflush(stdout);
while (1) {
pthread_mutex_lock(&mutexqueue);
fprintf(stdout,"mutex locked\n"); fflush(stdout);
while(STEQUESIZE == 0);
pthread_cond_wait(&condworker, &mutexqueue);
fprintf(stdout,"STEQUESIZE %d\n",STEQUESIZE);
fflush(stdout);
STEQUESIZE--;
pthread_mutex_unlock(&mutexqueue);
}
}
int main(int argc, char **argv) {
int nthreads = 1;
STEQUESIZE=0;
pthread_mutex_t mutexqueue = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condworker = PTHREAD_COND_INITIALIZER;
threadIDs = (pthread_t *)malloc(sizeof(pthread_t)*nthreads);
int j;
for(j=0; j< nthreads;j++)
pthread_create(&threadIDs[j], NULL, worker, NULL);
sleep(2);
pthread_mutex_lock(&mutexqueue);
STEQUESIZE=1;
pthread_cond_signal(&condworker);
pthread_mutex_unlock(&mutexqueue);
printf("done\n");
while(1);
return 0;
}
あなた 'pthread_mutex_t mutexqueue'と('メインで 'pthread_cond_t condworker'は)'同じ名前のグローバル変数をシャドウ。 – EOF