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-06-2008
This variable is using something like global memory. This memory is written
by a third party app, which takes care that, we have no collision between
two apps. But now I have two or more threads whitin my app, which will only
read this global memory.

HP

"Helge Kruse" <(E-Mail Removed)> schrieb im Newsbeitrag
news:4848f588$0$6606$(E-Mail Removed)-online.net...
>
> "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
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-06-2008
On Jun 6, 10:29 am, "Helge Kruse" <(E-Mail Removed)> wrote:
> "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.


The question said "only reading, never writing".

> My common understanding of variables are use to store data, so
> they will be written sometimes.


Before threading is started, for example. I have a lot of
variables which are only read, never written, once threading has
started.

> We would use a constant instead if they are not written.


Unless they need some sort of initialization, which can't be
done until main has been entered.

In practice, of course, a lot of the time, the variables will be
const.

> 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.


That's not the way I interpreted his question, but I agree that
it could have been clearer.

> 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.


You disagree with Posix?

> 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.


For a given compiler, on a given machine, you can sometimes
know. But it's irrelevant. Even if the variable is in a single
machine word, you need external synchronization. All Posix
offers here are the pthread_... functions; under Solaris, on a
Sparc, I'll occasionally drop down to assembler, and insert a
membar function myself, but only if it's really, really
necessary. And the compilers I use will never, under any
conditions, generate any membar instruction. (On IA-32
architecture, I think that the lock prefix---implied on the xchg
instruction, will automatically set up some sort of fence. But
again, it's never generated by the compiler, so you either have
to use inline assembler, or one of the system primitives.)

--
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 6, 11:14 am, "Hapa" <(E-Mail Removed)> wrote:
> This variable is using something like global memory. This
> memory is written by a third party app, which takes care that,
> we have no collision between two apps. But now I have two or
> more threads whitin my app, which will only read this global
> memory.


Which still isn't too clear: by global memory, do you mean
shared memory, with the third party application in a separate
process? And when you say that there is no collision between
the applications, do you mean that there will never be two
processes accessing this shared memory at the same time? If
your process has exclusive access to this shared memory, there
is no need for any synchronization between threads within the
process; all the memory synchronization you need has occured
when you acquired access to the shared memory. (At least under
Posix; I'll less sure about Windows.)

--
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?


>> 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.


> The question said "only reading, never writing".

And it said thread synchronization.

>> 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.


> That's not the way I interpreted his question, but I agree
> that it could have been clearer.

Yes, we are interpreting something. Probably we do it in a wrong way.

>> > 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.


>You disagree with Posix?

No, never wrote this, I never cited Posix. I disagree that the single memory
access is irrelevant. The fact if you have single memory access it
determines the robustness of the read operation.
With a single memory access it is just impossible that the problem I
described.

>> 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.


> For a given compiler, on a given machine, you can sometimes
> know.

You're right. But that's not "something you really can't know". Well, you
said you can know.

> But it's irrelevant. Even if the variable is in a single
> machine word, you need external synchronization.

If a read operation is in a single memory read operation, what do you want
to synchronize?

> I'll occasionally drop down to assembler, and insert a
> membar function myself...

Sorry, dont know this membar function.

> (On IA-32 architecture, I think that the lock prefix---implied
> on the xchg instruction, will automatically set up some sort of fence.

Yes, the lock prefix can avoid interrupt (and thread switch) for an
instruction.
No, the xchg instruction is only used for read and modify. The question
concerned a read-only access.

Regards,
Helge


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      06-06-2008
In article <b1949129-a110-4cba-9f8a-bc195dd0e8a3@
56g2000hsm.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...

[ ... ]

> 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.


Threads _often_ introduce cost -- then again, at times, they make a
design substantially _simpler_, particularly if the fundamental design
involves a lot of separate actions that rarely interact, and then only
in clearly specified ways.

Unfortunately, quite a few jobs aren't amenable to such a decomposition.
Even among those that are, the people writing the software may not use
it -- sometimes intentionally, others just because they missed it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-06-2008
On Jun 6, 1:19 pm, "Helge Kruse" <(E-Mail Removed)> wrote:
> "James Kanze" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...


[...]
> >> > 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.

> >You disagree with Posix?


> No, never wrote this, I never cited Posix.


But I did. The Posix standard says that whether the memory
accessed is a single word or not is irrelevant. If you
disagree, then you disagree with the Posix standard.

> I disagree that the single memory access is irrelevant. The
> fact if you have single memory access it determines the
> robustness of the read operation.


Conforming to the specifications of your platform and your OS
determines the robustness of the read operation. Some platforms
may give you special guarantees for single word accesses;
Sparcs under Solaris don't, however, and as far as I know, nor
do Intels under Windows. If you know otherwise, you're free to
point me to the documentation which specifies it---I've been
looking for something more detailed and precise for Windows for
a long time now.

> With a single memory access it is just impossible that the
> problem I described.


What problem? Whether the data is in a single word or not, you
don't have any guarantee from Posix that it will work.

> >> 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.

> > For a given compiler, on a given machine, you can sometimes
> > know.


> You're right. But that's not "something you really can't
> know". Well, you said you can know.


You can know whether the data is on a single word, or not,
sometimes, although such guarantees are rare. You can't know
whether that will help.

> > But it's irrelevant. Even if the variable is in a single
> > machine word, you need external synchronization.


> If a read operation is in a single memory read operation, what
> do you want to synchronize?


Memory.

> > I'll occasionally drop down to assembler, and insert a
> > membar function myself...


> Sorry, dont know this membar function.


It's the Sparc equivalent of a fence. It's used to synchronize
memory.

> > (On IA-32 architecture, I think that the lock
> > prefix---implied on the xchg instruction, will automatically
> > set up some sort of fence.


> Yes, the lock prefix can avoid interrupt (and thread switch)
> for an instruction.


At least on recent Intel processors, it does a lot more; it
generates some sort of implicit fence, ensuring memory
synchronization.

> No, the xchg instruction is only used for read and modify.


And it has an implicit lock prefix, at least on more recent
Intel processors. (This means that a single xchg instruction
will probably be slower than a series of mov instructions if you
don't need the synchronization.)

--
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
 
Ian Collins
Guest
Posts: n/a
 
      06-07-2008
Helge Kruse wrote:
> "James Kanze" <(E-Mail Removed)> wrote:
>
>> You disagree with Posix?


> No, never wrote this, I never cited Posix. I disagree that the single memory
> access is irrelevant. The fact if you have single memory access it
> determines the robustness of the read operation.
> With a single memory access it is just impossible that the problem I
> described.
>

Aside from the standards issues already raised, consider a single
*misaligned* memory access requiring more than one bus cycle. A context
switch may occur during such a read.

--
Ian Collins.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-08-2008
On Jun 7, 11:19 pm, Ian Collins <(E-Mail Removed)> wrote:
> Helge Kruse wrote:
> > "James Kanze" <(E-Mail Removed)> wrote:


> >> You disagree with Posix?

> > No, never wrote this, I never cited Posix. I disagree that
> > the single memory access is irrelevant. The fact if you have
> > single memory access it determines the robustness of the
> > read operation. With a single memory access it is just
> > impossible that the problem I described.


> Aside from the standards issues already raised, consider a
> single *misaligned* memory access requiring more than one bus
> cycle. A context switch may occur during such a read.


Note that the problem can be extremely complex. Some hardware
doesn't support byte reads and writes (at the memory access
level): writing a char involves reading the word, replacing the
byte in question, then rewriting it. There was some discussion
during standardization about what to do about those---in the
end, the decision was that the implementation has to make them
work, one way or another. Which may mean some form of locking
and external synchronization on some machines. (On the other
hand, individual bit fields in a common structure are *NOT*
independent objects.)

--
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