Re: Need Thread Advice

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks, The event object concept seems more reasonable. I will try that.

"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:q2cl22l14edhn05qtrq6mvequre9b99mql@xxxxxxxxxx
On Wed, 29 Mar 2006 10:36:11 -0500, "mike" <nospamplease.com> wrote:

I have an application that utilizes 2 threads for communications; 1 for
receive and 1 for transmit. The threads are derived classes of CWinThread.
The code is written so that when a button is clicked, I open a com port
and
immediately send data out the port. There was a timing problem in that
when
the port was opened, the threads were not running at the time that the
transmit operation was begun. To overcome this, I added a Ready flag in
the
threads and when I open the port, I check this flag to make sure the
thread
is running before exiting the open com function.

/* pseudo code */
OpenCom()
{
/*... open comm stuff */
pthread = CreateThread(......);

pthread->ResumeThread();

while ( !pthread->Ready ) ;
}

The debug code worked ok, but the release code hung in the while() loop. I
realized I had to free up time for the thread so I changed it to
while ( !pthread->Ready ) Sleep(5);

This seems to work. My question is, is there a better way to accomplish
this.

Yes. Your while-loop is what is known as a "busy loop". It is a loop that
consumes all CPU cycles for no good reason, and it should be replaced with
a method that isn't such a resource hog. The "better method" in this case
might involve an event object and WaitForSingleObject. As for your
debug/release mode discrepancy, consider that your loop calls no function
that could change pthread->Ready. In Release mode, the optimizer notices
this and assumes the variable never changes after the first time it tests
it. That is, it probably copies the variable into a register and checks
the
contents of the register from then on, and the result is an infinite loop
if Ready contained false at the moment it was copied to a register. You
could "solve" this problem by declaring Ready volatile, but there are much
better ways, such as using an event object. Note that while volatile is a
little less work, it doesn't do anything about the busy loop, and on some
multiprocessor architectures, there can be an indefinite delay between the
time one thread changes Ready and another thread notices the change. The
reason for this is volatile typically does not have the correct memory
barrier semantics for multithreaded programming, which can lead to some
very weird situations for threads executing on different CPUs.

--
Doug Harrison
Visual C++ MVP


.



Relevant Pages

  • Re: Arraylist Index Question
    ... need a loop. ... but the reason i just need to see if all ... my readings are equal to 4096.00 then i need to set a set condition ... error on each port. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: object system...
    ... but you shall not reason contradictory. ... while Halt loop ... Any finite halting problem is decidable, ...
    (comp.object)
  • Re: Which Is Better?
    ... The reason they gave was from a DBA perpspective like it was ... MERGE really necessary here - do you do any inserts in that loop, ... statistics for both approaches and compare them to see what could ... Did you capture any other statistics aside from run time ...
    (comp.databases.oracle.misc)
  • Re: Threading and Serial port issue
    ... I am using the Opennetcf Port class to handle the serial comms. ... I then sit in a loop waiting for data to arrive. ... private void SetupBoard_Click ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: measuring clock cycles per second
    ... In fact, I am not trying to convince you, I'm trying to find a reason why you would think it's useless. ... Let's assume that a program only executes a single loop and this loop ... Even if the average execution time of #1 is exactly 1/4 of the sampling interval and the average execution time of #2 is exactly 3/4 of the sampling interval, then, provided the standard deviation of the respective probability distributions of times is nonzero, the total time taken by the loop will, in general, be unequal to the sampling interval, even if by a mere 1%. ... Rather than the poor resolution, I find the fact that measurement itself leads to changes in timing more difficult to work with, especially when -g is used (not sure about the reason). ...
    (comp.os.linux.development.apps)