|
Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com |
From: Aidan Kehoe (kehoea
parhasard.net)Date: Sat Feb 24 2001 - 12:25:01 CST
On February 24, Dan Shechter wrote:
> Hi,
>
> Can two threads access at the same time the same byte/word/dword with out
> corrupting the data?
>
Well, they can't both write to it, but they should be able to read
from it. If you want them both to write to it, use a mutex;
#include <pthread.h>
[ ... ]
/* Do this when the thing is declared; either as a static variable
in a function, or at top level. */
pthread_mutex_t myvar_mutex = PTHREAD_MUTEX_INITIALIZER;
double myvar;
[ ... ]
/* write to variable */
pthread_mutex_lock(&myvar_mutex);
myvar = 47.943;
pthread_mutex_unlock(&myvar_mutex);
[ ...]
/* read from variable (no mutex fiddling needed). */
printf("value of myvar is %Le \n", myvar);
BTW, in this neck of the woods, `word' means `word size of the
machine' --> sizeof(int) * 8 . Not 16-bits-because-that's-the-way-
it-was-on-the-286. Although it does seem to be staying at 32 bits on
the Alpha and Sparc64, tho' I'd question that reasoning myself.
Also, this whole area is much more appropriate to
comp.unix.programmer; there's nothing inherently OpenBSD specific
about it.
- Aidan
-- "... We may stumble along the way but civilization, yes, the Geneva convention, chamber music, Susan Sontag, yes, civilization." -- Gremlins 2; The New Batch
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]