Ich möchte den Wert eines in einer Struktur gespeicherten Zählers erhöhen. Es werden 3 Threads in die Funktion tattoo_shop kommen, um die Anzahl der Personen zu erhöhen, aber aus bestimmten Gründen bleibt der Wert von number_of_people gleich.So erhöhen Sie den Wert einer Struktur mit mehreren Threads in C
Ich habe versucht, den Fall der Reihe nach zu reproduzieren, und es funktioniert. Gibt es etwas Besonderes zu tun, während ich mit Threads arbeite? Danke :)
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);
}
Ausgang:
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 eine lokale Variable ist, und jedes Mal, wenn die Routine aufrufen, ist es intialized neu – Ora
Es ist mir nicht klar, wie Sie Ihre mehrere Threads die gleiche Warteschlange teilen enden. –
@OliverCharlesworth meine Threads gehen alle zur Funktion tattoo_shop und von dort bekommen sie die Funktion add_to_the_queue. – MaxUt