![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2008
Posts: 7
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |