Re: Thread safe programming
Re: Thread safe programming
- Subject: Re: Thread safe programming
- From: Gareth White <email@hidden>
- Date: Wed, 5 Jun 2002 22:23:45 +0800
At 3:00 PM +0200 5/6/2002, Jens Bauer wrote:
>
If you only read the buffer, you should change the function call to this:
>
>
void *memchr(const void *src, unsigned char chr, size_t size)
Actually, if you're going to cast anyway, "const void *" isn't much more
useful than "void *", because it's very easy to forget to include "const"
in the type you're casting to.
For example:
const void *x;
void *y;
char *a = (char *)x; // compiles without any warning
char *b = (char *)y; // same as above
const char *c = (const char *)x; // this is probably want you wanted
const char *d = (const char *)y; // same as above
It'd be better to avoid "void *" altogether, and instead declare your src
as "const unsigned char *". Then you wouldn't have to cast at all, and
you'd get a warning when you tried to assign src to a non-const variable.
But like you said, this won't help make the function thread-safe. To do
that, you have to change the code so it doesn't modify at all the buffer
that's being searched. Then it would be safe to call the function with the
same arguments from two different threads - provided the buffer isn't also
being modified by some other thread at the same time!
When data is accessed by more than one thread, unless the data is
guaranteed to be immutable, you have to use some sort of synchronization
mechanism like locks/mutexes/semaphores when accessing it.
Hope that helps,
Gareth
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.