구조체에 저장된 카운터의 값을 증가시키고 싶습니다. 3 개의 스레드가 사람들의 수를 늘리기 위해 tattoo_shop이라는 글꼴로 들어 오지만 몇 가지 이유로 number_of_people의 값은 동일하게 유지됩니다.C에서 다중 스레드 구조 값을 증가시키는 방법
순차적으로 사례를 재현하려고 시도했는데 작동 중입니다. 쓰레드로 작업 할 때 뭔가 특별한 것이 있습니까? 당신에게 :) 감사
typedef struct {
int number_of_people;
}Queue;
void *tattoo_shop(void *arguments){
Client *args = arguments;
Queue the_queue;
add_to_the_queue(&the_queue,args);
}
void add_to_the_queue(Queue *the_queue, Client *the_client) {
pthread_mutex_lock(&mutex_queue);
the_queue->number_of_people++;
pthread_mutex_unlock(&mutex_queue);
printf("The thread %d is changing the counter of the queue which is now %d \n",the_client->id,the_queue->number_of_people);
}
출력 : Queue the_queue;
는 지역 변수가 아닌 공유 한 이후
The thread 1 is changing the counter of the queue which is now 1
The thread 0 is changing the counter of the queue which is now 1
The thread 2 is changing the counter of the queue which is now 1
the_queue는 지역 변수이며, 당신이 루틴을 호출 할 때마다, 그것은 – Ora
새롭게 그것은 당신의 여러 스레드가 같은 큐를 공유 결국 어떻게 나에게 분명하지 않다 intialized 것 : 약간의 조정은이 문제를 해결합니다. –
@OliverCharlesworth 내 스레드는 모두 tattoo_shop 함수에 있으며 그 함수에서 add_to_the_queue 함수를 사용합니다. – MaxUt