pthread and mutex question


 
Thread Tools Search this Thread
Top Forums Programming pthread and mutex question
# 1  
Old 11-15-2009
pthread and mutex question

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 idead to solve this problem?
thanks in advance Smilie

Last edited by Franklin52; 11-15-2009 at 02:34 PM.. Reason: Please indent your code and use code tags!!
# 2  
Old 11-15-2009
It might help (debug) if you printed the structural elements of args inside your void *print_the_arguments(void *arguments).
# 3  
Old 11-15-2009
Try the following
Code:
#include <stdio.h>
#include <pthread.h>

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

struct arg_struct {
  int deposit;
};

int total = 0;

void *print_the_arguments(void *arguments)
{

  struct arg_struct *args = (struct arg_struct *)arguments;

  pthread_mutex_lock(&lock);
  total = total + args -> deposit;
  printf("%d\n", total);
  pthread_mutex_unlock(&lock);

  pthread_exit(NULL);

}

int main()
{
  pthread_t some_thread1;
  pthread_t some_thread2;

  struct arg_struct args1;
  struct arg_struct args2;

  total = 5;

  args1.deposit = 3;
  pthread_create(&some_thread1, NULL, print_the_arguments, (void *)&args1);

  args2.deposit = 10;
  pthread_create(&some_thread2, NULL, print_the_arguments, (void *)&args2);

  pthread_join(some_thread1, NULL);
  pthread_join(some_thread2, NULL);
  pthread_exit(NULL);
}

# 4  
Old 11-16-2009
Quote:
Originally Posted by fpmurphy
Try the following
Code:
#include <stdio.h>
#include <pthread.h>
 
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 
struct arg_struct {
  int deposit;
};
 
int total = 0;
 
void *print_the_arguments(void *arguments)
{
 
  struct arg_struct *args = (struct arg_struct *)arguments;
 
  pthread_mutex_lock(&lock);
  total = total + args -> deposit;
  printf("%d\n", total);
  pthread_mutex_unlock(&lock);
 
  pthread_exit(NULL);
 
}
 
int main()
{
  pthread_t some_thread1;
  pthread_t some_thread2;
 
  struct arg_struct args1;
  struct arg_struct args2;
 
  total = 5;
 
  args1.deposit = 3;
  pthread_create(&some_thread1, NULL, print_the_arguments, (void *)&args1);
 
  args2.deposit = 10;
  pthread_create(&some_thread2, NULL, print_the_arguments, (void *)&args2);
 
  pthread_join(some_thread1, NULL);
  pthread_join(some_thread2, NULL);
  pthread_exit(NULL);
}

Great, now my program able to run the way it should be, now I can see what caused the problem

many Thanks for the help, really appreciate it

PROBLEM SOLVED
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

pthread question : global variable not updated

Hi, I wrote the following program to understand mutexes. If I run the program , number of threads is shown as zero, even after creating one thread. When running with gdb, it works fine. The function process is used to update global variable (used to keep track of threads). It looks like the... (2 Replies)
Discussion started by: sanjayc
2 Replies

2. Programming

Question (pthread): How to Signal all threads w/o "broadcast"?

Hello all, Is there any way that I can signal (wake) all threads that I have created without using pthread_cond_broadcast? Cheers! Aaron (6 Replies)
Discussion started by: mobility
6 Replies

3. Programming

Number of threads waiting on a pthread mutex/rwlock

Using pthreads is there a way to determine how many threads are waiting on a locked resource? I mean, once a shared resource is protected using e.g. pthread_rwlock_t or pthread_mutex_t one thread grabs the lock and other threads will go to sleep waiting for the resource to be available again. Is... (0 Replies)
Discussion started by: muggu
0 Replies

4. Programming

Mutex lock question

Hi all, I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation. Below is the summary of my code : MUTEX LOCK performTask(); MUTEX UNLOCK. In some cases I get into the situation where... (2 Replies)
Discussion started by: cjjoy
2 Replies

5. Programming

Mutex will not work

I am trying to use mutex in my multi-tread project, but they don't seem to work. I have created a simple demonstration of the problem. This is NOT how I would use a mutex, only a demonstration of the problem: #include <stdio.h> #include <pthread.h> int main() { int val; ... (3 Replies)
Discussion started by: ChrisWilliams
3 Replies

6. Programming

a question about pthread performance

Hello, I run my pthread code on Linux with 4 processors. However, the speed up is only 2 times. The code is about solving equation (G+s(i)C)z(i)=B*us(i), i=1,...,n. Here G,C are m*m matrix, B*us(i) is a m*1 vector and s(i) are n different numbers. I need to solve the equation n times to... (1 Reply)
Discussion started by: mgig
1 Replies

7. AIX

pthread performance question

Running dedicated on AIX with 4 processors, creating 4 threads, each with equal work to do, only runs about 20% faster than 1 thread with all of the work. Test case has no blocking but does share memory for read access only. Any ideas why I'm only seeing 20% gain? Is this typical on AIX? ... (1 Reply)
Discussion started by: ldarden
1 Replies

8. AIX

pthread lock question

Is it possible that the function "pthread_cond_broadcast" block itself and the function "pthread_cond_wait" unblock in multi-threads programming ? The operating system is AIX 5.2, its maintenance level is : 5.2.0.4, VisualAge C++ 6.0. Thanks (0 Replies)
Discussion started by: Frank2004
0 Replies

9. UNIX for Dummies Questions & Answers

mutex

Can anyone explain me what mutexes are in multithreading environment? (2 Replies)
Discussion started by: sagar
2 Replies

10. Programming

Threads and Mutex

Hi all, I am working in a UNIX/C environment. I would like to understand more about MUTEX and Threads. Can someone explain me these concepts and how they are related. Vijay (2 Replies)
Discussion started by: vthasan
2 Replies
Login or Register to Ask a Question