Re: To Context switch or Spin



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

[...]

.



Relevant Pages

  • Re: To Context switch or Spin
    ... core, two virtual cores. ... FSB, steals CPU execution resources, and dirties critical OS ... Then when someone upgrades their ...
    (comp.programming.threads)
  • Re: Alternative to Powerleap P4 processor upgrade
    ... the market for CPU upgrades was due to decline simply because clock multipliers ... powerleap did write back by they way. ... I know that generally processor upgrades just don't make sense these ...
    (alt.sys.pc-clone.dell)
  • Re: Nearly 100% CPU Usage
    ... what is the model number of this laptop? ... As Paul pointed out, ... RAM seems to show over 500 MB ... Can someone tell me if 100% CPU usage is the reason why everything ...
    (microsoft.public.windowsxp.general)
  • Re: SP3 Success - 2 down 22 to go
    ... But while the 1st two upgrades were problem-free, the 3rd machine made up for that. ... Details of the 3.2 GHz software/hardware where I've done most of my testing so far are below. ... I've been humbled too many times by upgrades, so I'm not here to gloat, just report that SP3 does work well for at least SOME of us. ... My next machine has an AMD 3500 CPU, so I've renamed the INTELPPM.SYS file in preparation to upgrade it. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: intel celeron
    ... It is an HP522n, the cpu is a Celeron model 1, stepping 3, running at ... it (the BIOS likes to ID the processor, ... of BIOS upgrades, to help home experimenters. ...
    (alt.comp.hardware.pc-homebuilt)