Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 5th, 2008, 9:13 AM   #1
mynickmynick
Newbie
 
Join Date: May 2008
Posts: 7
Rep Power: 0 mynickmynick is on a distinguished road
pthread safe library functions?

In linux using pthreads
When I call a whatever usual function like for instance:

thread 1
a=div(b,c).rem //a = div remainder of b div c

thread 2
x=div(y,z).rem

so I call the same function on different (not shared) data
Can I be sure that the two function calls by the two threads do not share any (internal) data (may be registers or global-library-shared) and so have no side unpredicted effect??

The same question holds for usual functions like sin() exp() pow() rand() nanosleep() etc.
mynickmynick is offline   Reply With Quote
Old May 6th, 2008, 6:15 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Re: pthread safe library functions?

With a multitasking OS (which is - sort of - a prerequisite for using pthreads) the operating system prevents basic operations on distinct data from interacting.

A lot of standard functions set the errno value (look up <errno.h> in the C standard) when an error occurs (eg argument out of range, overflow, etc), and errno is typically implemented using some global data. This means that, if you're using standard functions in a way that they may encounter an error, that they are not thread safe. That is certainly true for exp() and pow() and potentially true for other math functions like fmod(). That aside, functions that return a value based solely on arguments (no reliance on shared state) should not be a problem in multi-threaded code.

rand() can, if it is implemented using global data (not certain, but a possibility given that it returns a different value each time it is called), be a cause of problems if multiple threads call it.

nanosleep() is not a standard function, so I can't comment.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a New C++ Library King C++ 7 May 7th, 2007 9:11 PM
Discussion: How to implement the 'glue' between app and a library? Eoin C++ 10 Sep 28th, 2006 8:14 AM
what is the difference between an API and a library? linuxpimp20 Other Programming Languages 6 Aug 27th, 2005 7:25 AM
Exporting Functions victorsk Visual Basic 1 May 18th, 2005 2:32 PM
User-defined creatNode and deleteNode functions for a doubly-linked list jgs C 2 Apr 28th, 2005 8:53 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC