![]() |
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. |
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. |
| All times are GMT -5. The time now is 4:15 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC