From: Skybuck Flying on
Hello,

When is it safe to read/write from/to memory with multiple threads ?

For example:

Assume memory is like so:

Memory:

Byte 0, Byte 1, Byte 2, Byte 3, Byte 4, Byte 5, Byte 6, Byte 7, Byte 8, Byte
9... Byte N-1

I think it's only multi-threaded-safe to read the contents of one entire
memory cell ?

For example.

A memory cell could be 4 or 8 bytes ?

So then it's only safe to read 4 or 8 bytes aligned etc... ?

This is array related question...

Thanks for any insight you can give me I could use some ;)

Just wondering... no tests done yet ;)

Bye,
Skybuck.


From: Nick Maclaren on

In article <4a306$4858e6e4$541983fa$13076(a)cache3.tilbu1.nb.home.nl>,
"Skybuck Flying" <BloodyShame(a)hotmail.com> writes:
|>
|> When is it safe to read/write from/to memory with multiple threads ?

In almost all current programming languages, never.

Rumours have it that it can be done safely in Ada, and C++ is
working on it for the next standard. Beyond that, you are relying
on undefined behaviour.

On some architectures, it is possible in assembler. On others, it
isn't safe even in that.

There may be some compiler/system/library combinations for which
you can write safe, but completely non-portable, code. I don't know
of any, but can't exclude the possibility.

Well, you did ask :-)


Regards,
Nick Maclaren.
From: Terje Mathisen on
Nick Maclaren wrote:
> In article <4a306$4858e6e4$541983fa$13076(a)cache3.tilbu1.nb.home.nl>,
> "Skybuck Flying" <BloodyShame(a)hotmail.com> writes:
> |>
> |> When is it safe to read/write from/to memory with multiple threads ?
>
> In almost all current programming languages, never.

Or always, if you can make sure that each thread has totally private
memory areas, with no form of sharing at all, including false sharing
due to being in the same memory page or even the same TLB way?

Terje

--
- <Terje.Mathisen(a)hda.hydro.com>
"almost all programming can be viewed as an exercise in caching"
From: Nick Maclaren on

In article <D9idnU_W0vGmz8TVnZ2dnUVZ8vidnZ2d(a)giganews.com>,
Terje Mathisen <terje.mathisen(a)hda.hydro.com> writes:
|> Nick Maclaren wrote:
|> > In article <4a306$4858e6e4$541983fa$13076(a)cache3.tilbu1.nb.home.nl>,
|> > "Skybuck Flying" <BloodyShame(a)hotmail.com> writes:
|> > |>
|> > |> When is it safe to read/write from/to memory with multiple threads ?
|> >
|> > In almost all current programming languages, never.
|>
|> Or always, if you can make sure that each thread has totally private
|> memory areas, with no form of sharing at all, including false sharing
|> due to being in the same memory page or even the same TLB way?

I am not sure that you CAN arrange that in any existing programming
language! You can in assembler.


Regards,
Nick Maclaren.
From: David W Schroth on
Nick Maclaren wrote:
> In article <D9idnU_W0vGmz8TVnZ2dnUVZ8vidnZ2d(a)giganews.com>,
> Terje Mathisen <terje.mathisen(a)hda.hydro.com> writes:
> |> Nick Maclaren wrote:
> |> > In article <4a306$4858e6e4$541983fa$13076(a)cache3.tilbu1.nb.home.nl>,
> |> > "Skybuck Flying" <BloodyShame(a)hotmail.com> writes:
> |> > |>
> |> > |> When is it safe to read/write from/to memory with multiple threads ?
> |> >
> |> > In almost all current programming languages, never.
> |>
> |> Or always, if you can make sure that each thread has totally private
> |> memory areas, with no form of sharing at all, including false sharing
> |> due to being in the same memory page or even the same TLB way?
>
> I am not sure that you CAN arrange that in any existing programming
> language! You can in assembler.
>
>
> Regards,
> Nick Maclaren.

Well, it's not language specific, but on the 2200 Architecture, data
that is allocated on the stack is thread-private ...