On 6/2/07, Eduardo Morras <[EMAIL PROTECTED]> wrote:
At 17:18 31/05/2007, you wrote:
>On 5/31/07, Eduardo Morras <[EMAIL PROTECTED]> wrote:
>>At 23:25 30/05/2007, you wrote:
>>>Setting and reading individual bytes (u8 in sqlite-speak) are not
>>>threadsafe either. Only reading/setting entire entire words
>>>are threadsafe on most architectures.
>>
>>Using a uint32 for store the flags is threadsafe. There are less than 32 
true/false values and read/set is simple. I see no difference doing
>>
>>if (uint8==0){   // read/test bit
>>uint8=1;         // set bit
>>whatever more
>>}
>
>Not atomic, so not thread-safe.
>You have a race condition waiting to happen.
>
>>and
>>
>>if (uint32&&MASK){ // read/test bit
>>uint32&&=MASK;    // set bit
>>whatever
>>}
>
>Also not atomic, so not thread-safe.

I think i have not explained too well. I have wanted to point that if you can 
modify/set 8 or 9 values atomically/thread-safe you can do it with one 32 bit 
flag value.

Ok, but you can't do it in a portable way without some help (e.g. some
threads library, like pthreads, or the use of the Interlocked*
functions on Windows).

You have instructions on the x86 that you can use for this, but some
CPUs don't (many RISC ones), so you need to have some kind of memory
barrier until you complete the operation.

There are other things you may want to look at. For example, on the
x86, reading a 32 bit value from memory is only atomic if the address
is aligned, else you are actually doing 2 memory reads (meaning
another thread can change the value halfway). Other CPUs fix this by
simply not allowing un-aligned memory reads (causing a lot of problems
when porting x86 code to other platforms).

The subject is a bit more complex than this, because to really talk
about it we would also need to talk about what the C standard says
about the volatile keyword and how different compilers treat it. But
it's becoming off-topic.


Regards,
~Nuno Lucas

-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to