Re(2): Thread safe programming
Re(2): Thread safe programming
- Subject: Re(2): Thread safe programming
- From: Jens Bauer <email@hidden>
- Date: Wed, 5 Jun 2002 19:03:43 +0200
Hi Gareth,
On Wed, 5 Jun, 2002, Gareth White <email@hidden> 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.
Ofcourse, but it'll give you warnings in your compiler, which will show
you where to fix it.
>
For example:
>
>
const void *x;
>
void *y;
>
>
char *a = (char *)x; // compiles without any warning
>
char *b = (char *)y; // same as above
Don't. :)
>
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.
Indeed.
>
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.
Yes.
>
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
Uhm, it wasn't a problem for me, as I fixed this already - I didn't write
the original code. ;)
Love,
Jens
--
Jens Bauer, Faster Software.
-Let's make the World better, shall we ?
_______________________________________________
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.