Re: Explain this about threads

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
news:%23NKD4tU$HHA.4880@xxxxxxxxxxxxxxxxxxxxxxx
"Jon Slaughter" <Jon_Slaughter@xxxxxxxxxxx> wrote in message
news:av3Ji.8375$JD.7852@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"Instead of just waiting for its time slice to expire, a thread can block
each time it initiates a time-consuming activity in another thread until
the activity finishes. This is better than spinning in a polling loop
waiting for completion because it allows other threads to run sooner than
they would if the system had to rely solely on expiration of a time slice
to turn its attention to some other thread."



I don't get the "a thread can block each time...". What does it mean by
blocking? Does it mean that if thread B needs something from thread A
that thread A stops thread B from running until its finished but not
interfer with some other thread C?

Thanks,

Jon




Adding to what others have said in this thread:
1) You should never SpinWait on a single processor machine, doing so
prevents other threads in the system to make progress( unless this is
exactly what you are looking for).
Waiting for an event (whatever) from another thread in a SpinWait loop,
prevents the other thread to signal the event, so basically you are
wasting CPU cycles for nothing.
2) Define the count as such that you spin for less than the time needed to
perform a transition to the kernel and back, when waiting for an event.
Spinning for a longer period is just a waste of CPU cycles, you better
give up your quantum by calling Sleep(1) or PInvoke the Kernel32
"SwitchToThread" API in that case.


Ok, Tell me then how I can do clocked IO in a timely fashion without using
without spining?

Lets suppose I have do slow down the rate because it simply to fast for
whatever device I'm communicating with if I do not insert delays.

I would be really interested in knowing how I can do this without using
spinwaits because it is a problem that I'm having. It seems like its a
necessary evil in my case.

Basically I'm trying to do synchronous communication with the parallel port.
I have the ability to use in and out which is supplied by a kernel mode
driver and dll wrapper. So if I want to output some data to the port I can
do "out(data)" and it will output it

if I have something like

for(int i = 0; i < data.Length; i++)
out(data[i]);

Then this will run about 100khz or so(on my machine). Now what if I need to
slow it down to 20khz? How can I do this without using spin waits but still
do it in a timely fashion? IGNORE ANY DELAYS FROM TASK SWITCHING! I cannot
control the delays that other processes and task switching introduce so
since its beyond my control I have to ignore it. Whats important is the
upper bound that I can get and the average and not the lower bound. So when
I say it needs to run at 20khz it means as an upper bound.

for(int i = 0; i < data.Length; i++)
{
out(data[i]);
Thread.SpinWait(X);
}

Where X is something that slows this down enough to run at 20khz. I can
figure out X on average by doing some profiling. i.e., if I know how long
out takes and how long Thread.SpinWait(1) takes(on average) then I can get
an approximate value for X.

But how can I do this without using spin waits?





.



Relevant Pages

  • Re: Explain this about threads
    ... each time it initiates a time-consuming activity in another thread until the activity finishes. ... Waiting for an event from another thread in a SpinWait loop, prevents the other thread to signal the event, so basically you are wasting CPU cycles for nothing. ... Define the count as such that you spin for less than the time needed to perform a transition to the kernel and back, ... Spinning for a longer period is just a waste of CPU cycles, you better give up your quantum by calling Sleepor PInvoke the Kernel32 "SwitchToThread" API in that case. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH RFC 1/3] Add a trigger API for efficient non-blocking waiting
    ... There's little opportunity to put the CPUs into a low-power state. ... The trigger API allows the vCPUs to give up their CPU ... spinning means that the underlying hypervisor has no idea who's just ... loop even though there's runnable processes waiting. ...
    (Linux-Kernel)
  • Re: Question on routers and router tables
    ... A safety feature, sure, in that you don't have a dangerous bit spinning ... endlessly after you've finished your cut and you're either waiting to ... had a serious run in with a TS blade about 20 years ago. ...
    (rec.woodworking)
  • Waiting for Topper to find something besides a Display Name he did not like
    ... > Damn That twice I hit the Sent button instead of the Tagline button... ... >> Now I know you are spinning on me. ... I'll be waiting. ...
    (misc.transport.trucking)
  • Explain this about threads
    ... "Instead of just waiting for its time slice to expire, ... each time it initiates a time-consuming activity in another thread until the ...
    (microsoft.public.dotnet.languages.csharp)