내가 아는 한, 뮤텍스는 한 번만 잠근 다음 해제 될 때까지 다른 블록을 차단해야합니다. 여러 스레드가 같은 뮤텍스를 잠그는처럼 무수한 많은 시간 잠금
하지만 내 코드
, 그것은 보인다. 스레드 풀이 10이므로 확실하게 9가 차단되고 1이 잠길 것입니다. 그러나 나는이 산출물을 얻는다.Thread 0 got locked
Thread 1 got locked
Thread 3 got locked
Thread 4 got locked
Thread 2 got locked
Thread 5 got locked
Thread 6 got locked
Thread 7 got locked
Thread 8 got locked
Thread 9 got locked
내 뮤텍스는
pthread_mutex_t queuemutex = PTHREAD_MUTEX_INITIALIZER;
그리고 여기에 관련 코드 세그먼트이며, 같은 * .c 인 파일의 맨 위에 전역으로 정의된다.
//In the main function which creates all the threads
int k;
for (k = 0; k < POOLSIZE; k++) {
pthread_t thread;
threadinformation *currentThread = (threadinformation *)malloc(sizeof(threadinformation));
currentThread->state = (int *)malloc(sizeof(int));
currentThread->state[0] = 0;
currentThread->currentWaiting = currentWaiting;
currentThread->number = k;
threadArray[k] = currentThread;
pthread_create(&thread, NULL, readWriteToClient, threadArray[k]);
currentThread->thread = thread;
joinArray[k] = thread;
}
그리고 여기에 10 개의 스레드가 모두 잠기는 코드 세그먼트가 있습니다.
pthread_mutex_lock(&queuemutex);
fprintf(stderr,"Thread %d got locked \n",threadInput->number);
while((threadInput->currentWaiting->status) == 0){
pthread_cond_wait(&cond, &queuemutex);
fprintf(stderr,"Thread %d got signalled \n",threadInput->number);
}
connfd = threadInput->currentWaiting->fd;
threadInput->currentWaiting->status = 0;
pthread_cond_signal(&conncond);
pthread_mutex_unlock(&queuemutex);
'int retcode = pthread_mutex_lock (& queuemutex); assert (retcode == 0);' – Sebivor
'currentWaiting'이란 무엇입니까? 또는 더 구체적으로'currentWaiting-> status'의 초기 값은 무엇입니까? – Hasturkun