LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 12-07-2007, 07:30 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 461

Rep: Reputation: 31
making fuction thread safe


Hi all,
I'm trying to understand the idea of making a function only readable by one thread at once. I've got a function to read a commsport, I've set up a mutext the way I thought it should be, however my function is only read once, and then lock out. Can anyone advise?
I've set up the thread with

pthread_mutex_t rxmutex = PTHREAD_MUTEX_INITIALIZER

my fuction is

char RxChar(COMPORT *port)
{
char readletter[3];


pthread_mutex_lock( &rxmutex ); /*lock thread*/


if(read(port->id, readletter, 1)==-1) /* comms port has nothing*/
{
pthread_mutex_unlock( &txmutex); /*unlock*/
return(-1);
}
else
{
pthread_mutex_unlock( &rxmutex); /*unlock*/
return(readletter[0],readletter[0]);

}

pthread_mutex_unlock( &txmutex); /*just to be safe*/
return(-1);
}

my code work as if I remove the mutex stuff it's Ok. My understanding of threads self taught as I normally work on embedded stuff with interupts so I hope someone can explain this. It needs to be thread safe as the diffrent ports are called from diffrent threads. Thanks in advance.
 
Old 12-07-2007, 12:52 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Maybe you've got multiple threads all acquiring the lock ... but then failing to release it.

SUGGESTIONS:
1. Look at this man page:
https://computing.llnl.gov/tutorials...mutex_lock.txt

2. Consider "trylock" (instead of "lock").

3. Definitely check (all!) of your error return values, and respond appropriately.

'Hope that helps .. PSM

Last edited by paulsm4; 12-07-2007 at 12:54 PM.
 
Old 12-07-2007, 01:48 PM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 461

Original Poster
Rep: Reputation: 31
Ahhhh. I've just spotted it. But thanks for the reply

I lock the rxmutex, used for the recieve character, but unlock the txmutex used in the transmit function.

I worked on that for hours didn't seen an answer, then spotted it when I read my question :s
 
Old 12-08-2007, 12:02 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Yup - I should have noticed it myself :-)

Glad you're squared away.

And please *do* get in the habit of checking those error returns ;-)
 
  


Reply


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
making fuction thread safe knobby67 Programming 1 12-08-2007 08:51 AM
Thread-safe global variables in C JoeyAdams Programming 9 09-01-2007 10:55 AM
is Pam Thread Safe??? ptobra Programming 4 07-24-2007 09:19 PM
Reentrant and thread-safe code elyk Programming 2 01-04-2007 11:52 PM
What is thread safe....??? rajsun Programming 5 04-26-2005 11:33 PM


All times are GMT -5. The time now is 08:56 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