Re: To Context switch or Spin
- From: "Chris M. Thomasson" <no@xxxxxxxxxxxx>
- Date: Mon, 20 Oct 2008 22:45:43 -0700
"Paul" <spudWA@xxxxxxxxx> wrote in message news:56e76bf7-f8bb-4782-81aa-dc6257edcff0@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Oct 21, 11:54 am, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
> On Oct 20, 6:31 pm, Paul <spu...@xxxxxxxxx> wrote:
>
> > while (!TryEnterCriticalSection(cs))
> > Sleep(0); ; hope CS is unlocked next time I'm scheduled
>
> This is about the worst possible thing you can do. You are spinning
> tightly around a very expensive system call that accesses shared
> resources.
>
> Consider the following: We have a hyper-threaded CPU. One physical
> core, two virtual cores. The thread that spins and the thread that
> holds the lock are the only ready-to-run threads. They're running in
> the two virtual cores. In this case, the 'Sleep(0);' does not actually
> yield (since there is nothing to yield to). Instead, it saturates the
> FSB, steals CPU execution resources, and dirties critical OS
> structures in the cache.
>
> You really can't write your own synchronization primitives unless you
> know *exactly* what you're doing. There are way too many pitfalls.
>
> The other problem is that you might have written code that fails badly
> under HT before HT was developed. Then when someone upgrades their
> CPU, your code slows to a crawl. If you use the OS primitives, you can
> expect the OS and threading library to be upgraded to support the CPU.
> But having to track down the vendors to upgrade applications (whose
> vendors may not even understand the problem or still have the source
> code to fix it) is unreasonable.
Answers like this make me feel really overwhelmed by MT.
AFAICT, DS has written the truth indeed. Humm... Okay:
When your in a situation in which your Boss is not breathing down your neck for a "band-aid" type solution, I would take David's advise to heart.
Really; the try-lock method will fix your problem at hand wrt your Bosses requirements... But, its not recommended. That's why I wrote:
Its a shame.
Paul, I feel your pain; OUCH!
[...]
.
- References:
- To Context switch or Spin
- From: spudWA@xxxxxxxxx
- Re: To Context switch or Spin
- From: Chris M. Thomasson
- Re: To Context switch or Spin
- From: Paul
- Re: To Context switch or Spin
- From: David Schwartz
- Re: To Context switch or Spin
- From: Paul
- To Context switch or Spin
- Prev by Date: Re: To Context switch or Spin
- Next by Date: Re: To Context switch or Spin
- Previous by thread: Re: To Context switch or Spin
- Next by thread: Re: To Context switch or Spin
- Index(es):
Relevant Pages
|