C++ exception influence real time behaviour of CE



Hi,

We have found a very strange behaviour of WinCE. When a low priority
thread is causing a simple C++ exception it may happen, that high
priority threads are not sheduled properly during that time. One can
simple reproduce this with the following example.

Create a high priority thread, that does nothing else than incrementing
a counter every 10 msecs. In your main application thread with priority
251 (default) insert a while loop that does nothing else then throwing
and catching C++ exceptions. Measure the time your while loop takes (e.
g. 20 secs) and then compare it with the counter difference between
start point of while loop and end of while loop. You will see that
there the counter is not showing enough increments.

In our example throw 50000 exceptions. That takes us about 20 secs. Our
printfs show ~20000 ticks in the output --> this is correct.
But the output of the counter difference of the high priority thread
shows only a value of ~1000.

In our opinion it should show the number of ticks / 10 (that would be
abour 2000).

Please note that we know that exceptions shouldn't be used in such a
way and we don't do it. But we have special case where we see that we
loose serial port data because C++ exceptions occur in an application,
we have no control of.

Here is our example code:

HANDLE hThread = INVALID_HANDLE_VALUE;
bool bStop = false;
volatile DWORD dwCounter = 0;

class SerExcept : public std::exception
{
public:
SerExcept()
{
}

};

DWORD CounterThread(LPVOID)
{
HANDLE hEvent = CreateEvent( NULL, FALSE, TRUE, NULL );
while (!bStop)
{
WaitForSingleObject( hEvent, 10 );
InterlockedIncrement( (LPLONG)&dwCounter );
}
ReleaseMutex( hEvent );
CloseHandle( hEvent );
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
hThread = CreateThread( NULL, 0, CounterThread, 0, 0, NULL );

printf( "Set Counter thread priority to 0\n");
CeSetThreadPriority( hThread, 0 );

printf( "Start count\n" );
DWORD dwStartTick = GetTickCount();
DWORD dwStartCounter = dwCounter;

for (int iCount = 0; iCount < 50000; ++iCount)
{
try
{
SerExcept tmpSerExcept;
throw tmpSerExcept;
}
catch( const SerExcept& e )
{
}
}
printf( "Duration: %d counts\n", dwCounter - dwStartCounter );
printf( "Duration: %d ticks\n", GetTickCount() - dwStartTick );

bStop = true;

printf( "waiting for CounterThread() to terminate" );
WaitForSingleObject( hThread, 10000 );
CloseHandle(hThread);

printf( "exit\n" );
return 0;
}

Thankx
Roman

.



Relevant Pages

  • Re: Thread context switching
    ... > worker threads and PostMessage to wake the lower priority main UI thread ... > pre-empted by the lower priority threads. ... > assumption that higher priority thread would never be pre-empted by lower ... > occurred only when the high priority thread enters wait state. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: threads that wont yield
    ... >> In a simple pthreads app, I start a high priority thread and a low priority ... If the client thread starts running before acceptis called, ...
    (comp.unix.solaris)
  • Re: Thread context switching
    ... while your lower priority thread is running on another. ... >> worker threads and PostMessage to wake the lower priority main UI thread ... >> occurred only when the high priority thread enters wait state. ...
    (microsoft.public.win32.programmer.kernel)
  • CriticalSection & Priority Inversion problem
    ... There are some high priority threads and low priority ... where *ALL* threads execute the *same* function ... .Here i think high priority thread will pre- ...
    (microsoft.public.windowsce.embedded.vc)

Loading