Re: Thread - Shared Memory
From: Doug Pardee (dougpardee_at_yahoo.com)
Date: 07/28/04
- Next message: Anony!: "Re: StringTokenizer"
- Previous message: Sudsy: "Re: Struts Architecture"
- In reply to: Rob Shepherd: "Thread - Shared Memory"
- Next in thread: Rob Shepherd: "Re: Thread - Shared Memory"
- Reply: Rob Shepherd: "Re: Thread - Shared Memory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Anony!: "Re: StringTokenizer"
- Previous message: Sudsy: "Re: Struts Architecture"
- In reply to: Rob Shepherd: "Thread - Shared Memory"
- Next in thread: Rob Shepherd: "Re: Thread - Shared Memory"
- Reply: Rob Shepherd: "Re: Thread - Shared Memory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|