構造体に格納されているカウンタの値をインクリメントしたい。 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);
}
:)ありがとう出力:
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