I want to change the struct variable in the thread function as global.
But this doesn't work:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 1
typedef struct {
int id;
int reservable;
} Customer;
void *CustomerFunc(void *customer) {
Customer *a;
a = (Customer *)customer;
printf("reservable %d", a->reservable);
a->reservable = 5;
}
int main (int argc, char *argv[]) {
pthread_t abc[NUM_THREADS];
int rc;
long t;
Customer *customer = (Customer*)malloc(sizeof(Customer));
customer->id = 1;
customer->reservable = 2;
rc = pthread_create(&abc[0], NULL, CustomerFunc, (void *)&customer);
printf("reservable MAIN%d", customer->reservable);
pthread_exit(NULL);
}
This code result is:
reservable: 0 reservable MAIN: 2
But I want to see:
reservable: 2 reservable MAIN: 5