LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 06-11-2010, 06:43 PM   #1
kvm1983
Member
 
Registered: Jul 2009
Posts: 45

Rep: Reputation: 0
multithreading question


Hello all,

I have a multithreaded C program where I do signaling using condition variables at various points.

For signaling using posix condition variables, this works:
Code:
pthread_cond_signal(&(struct1->struct2[1].cond_var), &(struct1->struct2[1].mutex));
but this doesnt work:
Code:
my_cond_var = struct1->struct2[1].cond_var;
my_mutex = struct1->struct2[1].mutex;
pthread_cond_signal(&my_cond_var, &my_mutex);
Why does this happen? Why doesnt copying the mutex and the condition variable to another set of variables work?

Thanks for your help.

- Kshitij

Last edited by kvm1983; 06-11-2010 at 06:44 PM. Reason: Changing title
 
Old 06-11-2010, 07:55 PM   #2
ichrispa
Member
 
Registered: Mar 2005
Location: Dresden, Germany
Distribution: OpenSuse 11.2/3, Debian 5.0 , Debian 1.3.1, OpenBSD
Posts: 260

Rep: Reputation: 31
It wouldn't hurt to see the definitions of my_cond_var and the error.

If your my_cond_var and my_mutex are pointers, try removing the "address off"'s like so:

pthread_cond_signal(my_cond_var, my_mutex);

Otherwise you might be passing the address of the pointer.
 
Old 06-12-2010, 04:10 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
from "programming with posix threads"

Quote:
you should never make a copy of a condition variable, because the result
of using a copied condition variable is undefined.
 
1 members found this post helpful.
Old 06-14-2010, 11:49 PM   #4
kvm1983
Member
 
Registered: Jul 2009
Posts: 45

Original Poster
Rep: Reputation: 0
ichrispa,
I have made sure that I am not passing incorrect values to the function (pointers instead of addresses and so on), so thats not the problem for sure.
As bigearsbilly suggested, looks like its not a good idea to make a copy of the condition variable.

Thank you,
Kshitij
 
Old 06-15-2010, 06:58 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
yes.
you shouldn't make copies of anything in pthreads.
mutexes too.
it may work it may not, but behaviour is generally undefined.
 
  


Reply

Tags
condition, mutex, posix, threading


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
qt multithreading? suicidle_p0ptart Linux - Software 5 08-28-2007 01:54 AM
programming UI with multithreading (question) Huffsalot Programming 2 01-18-2006 01:41 AM
PHP Multithreading dlublink Linux - Software 1 10-25-2005 12:23 PM
Multithreading jayashrik Programming 1 07-20-2005 04:13 AM
multithreading libraries Pres Programming 3 06-20-2003 03:27 AM


All times are GMT -5. The time now is 09:02 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration