Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: multithreading question (OT)

Author: Robert Hyatt

Date: 12:54:19 03/02/01

Go up one level in this thread


On March 02, 2001 at 10:03:58, Pat King wrote:

>On March 02, 2001 at 08:45:51, Robert Hyatt wrote:
>
>>On March 02, 2001 at 08:39:57, Pat King wrote:
>>
>>>if sizeof(somebuiltintype) <= sizeof(sig_atomic_t), is it garunteed that reads
>>>and writes of somebuiltintype are atomic?
>>>
>>>thanks.
>>>
>>>Pat
>>
>>
>>I don't think it matters.  You _first_ use an atomic operation to lock the
>>data in question, then you don't have to worry about whether the read/write
>>is atomic.  IE my hash entries are 16 bytes so that would probably never be
>>an atomic read/write.
>
>But if the data in question is very simple (as in this case setting a bool flag
>or two) then a lock seems overkill. certainly almost any struct wouldn't be, but
>what of a bool or a char? For instance, my compiler (mingw) has sig_atomic_t as
>a typedef int, 4 bytes. so is anything 4 bytes or smaller atomic?


That won't work.  To set a flag, you have to load, OR, and store the word.  If
you don't protect it with a lock, another thread can also load, modify and
store the same word and lose your change, or vice-versa.

_any_ shared data has to be protected in some way from this problem.  The atomic
lock (semaphore/etc) is the way to do this.  Otherwise you will _always_ have a
bug that will pop up at random times.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.