Re: Thread - Shared Memory

From: Doug Pardee (dougpardee_at_yahoo.com)
Date: 07/28/04


Date: 27 Jul 2004 16:08:11 -0700

Rob Shepherd wrote:
> Whilst trying to find a decent/simple/lightweight (blah blah) way to achieve shared memory
> IPC between Threads I figure the ONLY way to do it is through
> synchronization. Hereby forcing the Thread to synchronize it's *operating* memory space
> with the *main* memory heap.
>
> Is there really no other way to do Shared Memory IPC without synchronization?

No, there is not.

Well, if you're running on a single-CPU system you can cover your eyes
and pretend, and it'll probably work.

But when you've got multiple CPUs working, SOMETHING has to tell one
CPU that the data that it's holding in registers and cache are needed
by another CPU, and to tell the other CPU that the data that it's
holding in *its* registers and cache are no longer valid because
another CPU has changed them. That "something" is a "memory barrier",
and in Java there is exactly one way to get memory barriers: the
"synchronized" keyword.

When you exit a synchronized block or method, the CPU's registers are
stored into memory and the CPU's write-back cache (if any) is
instructed to write all of its data into the shared memory. The lock
is not released until all data has completed being written to shared
memory.

When you enter a synchronized block or method, after the lock has been
obtained the CPU's cache is flushed to assure that it contains no
stale data, and the CPU's registers are assumed to contain nothing of
value.

This is really nothing special to Java. Any system that has to share
data among multiple CPUs has the same problem.

> I have lots of threads, passing lots of little messages, there's not a
> great deal of room for redesign.

You're pretty much stuck, then. Threads are lighter-weight than
processes, but they are far from weightless. Having more than a few
threads will set your program up for performance problems. Again, this
is nothing special to Java.

BTW, extensive multi-threading is a big no-no on OpenBSD, or so I've
heard.



Relevant Pages

  • Re: FPGA-based hardware accelerator for PC
    ... I know that in most cases the CPU ... that it contsins no cache, as BRAMs are too precious resources to be wasted ... The BRAMs are what define the opportunity, ... many threads with full associativity of memory lines using hashed MMU ...
    (comp.arch.fpga)
  • Re: Adjusting PC Hyperthreading for Spice Simulation
    ... Memory access taking hundreds of cycles? ... ago), 350 CPU cycles for a code cache miss was not atypical, but RAM ...
    (sci.electronics.design)
  • Re: Cost of calling a standard library function
    ... It accesses/reads memory using esi 4 ... > safly move it within the cache, without having to go via ebx. ... try it the same thing on a different earlier CPU, ... should check it out...for "tight inner loop" stuff, ...
    (alt.lang.asm)
  • Re: Adjusting PC Hyperthreading for Spice Simulation
    ... Memory access taking hundreds of cycles? ... ago), 350 CPU cycles for a code cache miss was not atypical, but RAM ...
    (sci.electronics.design)
  • Re: What can I check to fix system performance?
    ... it seems you have plenty of memory available: ... copies of files you have read of written lately, in a cache, in case ... processes per CPU, or 40 in all. ... Consider the disk structure. ...
    (comp.os.linux.setup)

Loading