LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2009, 12:33 PM   #1
Michael23
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
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 ideas to solve this problem?
thanks in advance

Last edited by Michael23; 11-15-2009 at 12:46 PM.
 
Old 11-15-2009, 12:40 PM   #2
Michael23
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Original Poster
Rep: Reputation: 0
sorry double post...

Last edited by Michael23; 11-15-2009 at 12:42 PM. Reason: double post
 
Old 11-16-2009, 03:53 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well, for a kick off it doesn't compile.

also you have your lock and unlock arse about face.
you:
1. lock
2. do some stuff
3. unlock

also your incorrect lock (which should be an unlock) comes after your thread exit.
and you are creating 2 threads with the same thread_t
therefore you are only joining one one thread.

apart from that it's fine

Last edited by bigearsbilly; 11-16-2009 at 03:55 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with locking pthread mutex johnbellone Programming 4 03-20-2009 12:43 PM
A question about pthread Stephen.Xin Programming 1 08-22-2006 11:26 PM
Pthread.... stuffs ... onwer of mutex rajsun Programming 1 07-26-2005 07:40 AM
How does pthread_mutex_lock() lock mutex in pthread icoming Programming 0 12-04-2004 08:54 AM
pthread mutex issue gauge73 Programming 6 04-20-2004 05:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration