Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Only reading of variable vs. thread synchronisation

Reply
Thread Tools

Only reading of variable vs. thread synchronisation

 
 
Hapa
Guest
Posts: n/a
 
      06-04-2008
Does only reading (never writing) of a variable need thread synchronisation?
Thanks for help?

PS.
Anybody knows a Visual C++ news group?



 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-04-2008
Hapa wrote:
> Does only reading (never writing) of a variable need thread synchronisation?
> Thanks for help?
>

That depends on whether you depend on the value read being up to date.

c.p.threads is a better place to ask, a similar question was asked there
recently check the archive.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-04-2008
On Jun 4, 11:32 am, "Hapa" <(E-Mail Removed)> wrote:
> Does only reading (never writing) of a variable need thread
> synchronisation?


Technically, it depends on the system, but under Posix (and I
think Windows as well), as long as no thread writes the object,
no synchronization is needed. As soon as any threat writes, all
accesses (even from threads which don't write) must be
synchronized.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Helge Kruse
Guest
Posts: n/a
 
      06-05-2008

"Hapa" <(E-Mail Removed)> wrote in message
news:g25nga$192$00$(E-Mail Removed)-online.com...
> Does only reading (never writing) of a variable need thread
> synchronisation?
> Thanks for help?
>
> PS.
> Anybody knows a Visual C++ news group?
>

It depends. When the variable access can be interrupted a thread switch can
occure. If you need more than one instruction to read _the_actual_data_ you
can read one part of the variable before and one after the read. This can
give strange results.

Where you need more than one instruction? Look at this variable named foo:

struct
{
int[100] a;
char b;
} foo;

/Helge


 
Reply With Quote
 
Gerhard Fiedler
Guest
Posts: n/a
 
      06-05-2008
On 2008-06-04 15:59:30, James Kanze wrote:

> As soon as any threat writes, [...]


thread === threat ?

Gerhard
 
Reply With Quote
 
Gerhard Fiedler
Guest
Posts: n/a
 
      06-05-2008
On 2008-06-05 05:59:58, Helge Kruse wrote:

>> Does only reading (never writing) of a variable need thread
>> synchronisation?
>>

> It depends. When the variable access can be interrupted a thread switch
> can occure. If you need more than one instruction to read
> _the_actual_data_ you can read one part of the variable before and one
> after the read. This can give strange results.


Can you explain this more? Maybe an example? (Considering that no writes to
the variable in question occur during the observation period.)

Thanks,
Gerhard
 
Reply With Quote
 
Helge Kruse
Guest
Posts: n/a
 
      06-05-2008

"Gerhard Fiedler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2008-06-05 05:59:58, Helge Kruse wrote:
>
>>> Does only reading (never writing) of a variable need thread
>>> synchronisation?
>>>

>> It depends. When the variable access can be interrupted a thread switch
>> can occure. If you need more than one instruction to read
>> _the_actual_data_ you can read one part of the variable before and one
>> after the read. This can give strange results.

>
> Can you explain this more? Maybe an example? (Considering that no writes
> to
> the variable in question occur during the observation period.)


The thread switch can occur at any point of time, after each CPU
instruction. A normal read operation will prepare some address registers and
perform than the memory access. That is no problem, since the variable is at
the same address. But when the size of the variable is greater than the CPU
can fetch in one memory cycle you are in trouble.

See this example with pseudo code. Your variable is a 64 bit integer. You
have a CPU with one 32 bit address register R1 and some 32 bit data
registers R0,R3,R4. Reading the 64 bit integer is performed in this way. For
illustration purposes I use a simple if-statement.

; _int64 some_variable;
; if (0 == some_variable) { do something fatal }

1: move R1,offset some_variable ; loads 32 bit address to R1
2: move R3,@(R1) ; read low 32 bit part
3: move R4,@(R1+4) ; read high 32 bit
4: or R0,R3,R4 ; set any bit in R0 where the bit in R3
or
; R4 is set. R0 is 0 if no bit was set.
5: jump_if_not_zero some_where
6: ; here starts somthing fatal
.....
X: ; this is some_where

Each numbered line is one CPU instruction. The thread switch can occure
after each line.

If some_variable has a zero value, the "do something fatal" block should be
executed. Now lets set some_variable to 0x0000000100000000. Another thread
will decrement some_variable when it gets the CPU. If the thread switch
occures after line 2, you will read the low part of some_variable to R3.
When the working thread gets the CPU again, line 3 is executed. The value of
some_variable is now 0x00000000FFFFFFFF.

You will see that R3 is 0 since the low part was 0 before the thread switch
and R4 is 0 since the high part is 0 after the thread switch. You would say,
that some_variable was never zero four hours. But "do something fatal" will
be executed in the case. -- unexpected, unsecure, not threadsafe.

Regards,
Helge


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-06-2008
On Jun 5, 3:20 pm, Gerhard Fiedler <(E-Mail Removed)> wrote:
> On 2008-06-04 15:59:30, James Kanze wrote:


> > As soon as any threat writes, [...]


> thread === threat ?


Now there's a freudian slip if I ever saw one. Threads do
introduce additional complexity, especially with regards to
ensuring correctness, and shouldn't be used unless the cost of
avoiding them outweighs this cost. (Like a lot of other
things---DLL's, TMP, etc.---, there seems to be a mode of using
them systematically, in every application, when a lot of the
time, you can do very well without them, with the results an
application that is simpler and more robust.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-06-2008
On Jun 5, 4:43 pm, "Helge Kruse" <(E-Mail Removed)> wrote:
> "Gerhard Fiedler" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On 2008-06-05 05:59:58, Helge Kruse wrote:


> >>> Does only reading (never writing) of a variable need
> >>> thread synchronisation?


> >> It depends. When the variable access can be interrupted a
> >> thread switch can occure. If you need more than one
> >> instruction to read _the_actual_data_ you can read one part
> >> of the variable before and one after the read. This can
> >> give strange results.


> > Can you explain this more? Maybe an example? (Considering
> > that no writes to the variable in question occur during the
> > observation period.)


> The thread switch can occur at any point of time, after each
> CPU instruction.


We all know that, but it's irrelevant here. The question
concerned what happens if no one writes to the variable. If no
thread modifies the variable, Posix (and Windows as well, I
think) guarantees that non-synchronized accesses work. You
don't need to know or understand more than that---the compiler
will do whatever is necessary for them to work. If any thread
modifies the variable, both Posix and Windows require
synchronization. Even if the variable is in a single machine
word (something you really can't know). Whether the variable is
in a single machine word or not is totally irrelevant here.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Helge Kruse
Guest
Posts: n/a
 
      06-06-2008

"James Kanze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>> >>> Does only reading (never writing) of a variable need
>> >>> thread synchronisation?



>> The thread switch can occur at any point of time, after each
>> CPU instruction.


> We all know that, but it's irrelevant here. The question
> concerned what happens if no one writes to the variable.


Probably we will have to ask the OP what the question concerned. I
understand that
- there are threads
- one thread is only reading
- it's in question if the reading threads need any synchronization.

My common understanding of variables are use to store data, so they will be
written sometimes. We would use a constant instead if they are not written.
Since threads are in question I assumed that a write access can occur in
another thread. I read the (never writing) related to the the current
thread.

Hapa, can you clearify?

> If any thread modifies the variable, both Posix and Windows
> require synchronization. Even if the variable is in a single
> machine word (something you really can't know). Whether the
> variable is in a single machine word or not is totally
> irrelevant here.


I disagree. After you have read a variable you have a copy of it (at least
to a CPU register). You have to define if a change after the read is
acceptible or not. Of not you need a thread synchronization. But even if it
is acceptible you can get in trouble as shown.

The compiler defines how data is stored. The statement, if a variable is in
a single word is compiler and CPU dependend. But you can know.


Regards,
Helge


--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple programs to deal with data format, data synchronisation Ram VHDL 1 02-24-2005 05:34 PM
Synchronisation problem Olivier Merigon Java 1 07-29-2004 02:43 AM
synchronisation JULES Firefox 0 04-20-2004 07:39 AM
Session End Synchronisation Glenn ASP .Net 1 10-31-2003 12:43 AM
data synchronisation - java server side or www server side? Thor Java 1 07-02-2003 05:44 PM



Advertisments