Re: Threads and C functions
Re: Threads and C functions
- Subject: Re: Threads and C functions
- From: Chaz McGarvey <email@hidden>
- Date: Thu, 10 Feb 2005 19:33:31 -0700
On Feb 10, 2005, at 5:03 PM, Ken Tozier wrote:
Hi all,
I wrote some C utility functions that are shared by a number of
classes but don't know enough about threads to know if I have to do
anything special with the functions to make them thread safe.
If two non-interacting threads call the same C function with different
data, are there any inherent problems? Or does the compiler take care
of access to the function?
The compiler will do nothing to make your functions thread-safe. It is
perfectly possible that two threads can be running any given function
at the same time. Most of the time it's not a problem. It can be a
problem if:
- the function accesses global memory.
- the function has static variables.
int myGlobalVar = 3;
int MyFunc()
{
static char buffer[256]; // this is bad
myGlobalVar = 63; // this is bad
}
Your functions can also be considered to not be thread-safe if they
call other functions which are known to not be thread-safe, like
inet_ntoa().
If any of these cases are true, you will want to either use locks in
your functions as needed or make sure it is well documented that those
functions are not thread-safe so that others will know to use locks.
There may be other thread-safety issues that others can point out. I'm
not very much of a threading expert either.
Chaz McGarvey
http://www.brokenzipper.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden