Hello,
I have got some issue with the struct variable with passed arguments
the variable in the sturct is only recognize the last value their assigned to
I'm pretty confused why the mutex didn't work out
here is my program:
Code:
#include<stdio.h>
#include<pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct arg_struct {
int arg1;
int deposit;
};
void *print_the_arguments(void *arguments)
{
pthread_mutex_unlock(&lock);
struct arg_struct *args = arguments;
int j;
j = args -> deposit;
printf("%d\n", args -> arg1 + j);
pthread_exit(NULL);
return NULL;
pthread_mutex_lock(&lock);
}
int main()
{
pthread_t some_thread;
struct arg_struct args;
args.arg1 = 5;
args.deposit = 3;
//pthread_mutex_unlock(&lock);
pthread_create(&some_thread, NULL, &print_the_arguments, (void *)&args) != 0;
//pthread_mutex_lock(&lock);
args.deposit=10;
pthread_create(&some_thread, NULL, &print_the_arguments, (void * &args) != 0;
return pthread_join(some_thread, NULL);
}
I'm expect the result would like this
8
18
instead I've got this
15
15
any ideas to solve this problem?
thanks in advance