www.digitalmars.com         C & C++   DMDScript  

c++.windows.32-bits - Multithreading problem

reply "Heinz-Peter Nüttgens" <hnuettgens t-online.de> writes:
Hello,

perhaps someone can help?

In my applications I use several threads. The main one
handles user in- and output, window handling, drawing
and so on. Then i've got a thread that communicates with
some piece of hardware. This thread is started by the first
one and depending what kind of task is to be done the first
waits for the second to finish. And that's the point.

Here is the routine, which should enable the other threads
to get processor time:

VOID wait_for_thread( ULONG ms)
{
        clock_t  t;

    if( ms ) {
        t  =  ((clock_t)ms * CLK_TCK) / 1000L;
        t +=  clock();
    } else {
        t = 0;
    }

    do {
        /* version 1 */
        (VOID)MsgWaitForMultipleObjects( 0L, NULL, FALSE, 0L, 0L);
        /* version 2 */
        // sleep(0L);
    } while( ms && clock() < t );

    return;
}

This routine should only wait for n milliseconds and during that time period
enable other threads to do their work, but it should finish as soon as
possible.

Version 1 is now working quite well but not perfect. I tried version 2 (I
know only
for threads that don't produce messages to be handled). But this one was
much slower
and delayed the whole application. Mostly this wait routine is called with 0
(zero) ms.
I do that just to initiate task switching, because the threads depend on
hardware drivers
which themselve need processor time.

Perhaps there is a big mistake in my theory to handle multiple threads?

Greetings

Heinz-Peter
Jan 29 2004
parent Jan Knepper <jan smartsoft.us> writes:
Why don't you just wait for the thread with WaitForSingleObject / 
WaitForMultipleObjects with the thread handle?

Jan



Heinz-Peter Nüttgens wrote:

 Hello,
 
 perhaps someone can help?
 
 In my applications I use several threads. The main one
 handles user in- and output, window handling, drawing
 and so on. Then i've got a thread that communicates with
 some piece of hardware. This thread is started by the first
 one and depending what kind of task is to be done the first
 waits for the second to finish. And that's the point.
 
 Here is the routine, which should enable the other threads
 to get processor time:
 
 VOID wait_for_thread( ULONG ms)
 {
         clock_t  t;
 
     if( ms ) {
         t  =  ((clock_t)ms * CLK_TCK) / 1000L;
         t +=  clock();
     } else {
         t = 0;
     }
 
     do {
         /* version 1 */
         (VOID)MsgWaitForMultipleObjects( 0L, NULL, FALSE, 0L, 0L);
         /* version 2 */
         // sleep(0L);
     } while( ms && clock() < t );
 
     return;
 }
 
 This routine should only wait for n milliseconds and during that time period
 enable other threads to do their work, but it should finish as soon as
 possible.
 
 Version 1 is now working quite well but not perfect. I tried version 2 (I
 know only
 for threads that don't produce messages to be handled). But this one was
 much slower
 and delayed the whole application. Mostly this wait routine is called with 0
 (zero) ms.
 I do that just to initiate task switching, because the threads depend on
 hardware drivers
 which themselve need processor time.
 
 Perhaps there is a big mistake in my theory to handle multiple threads?
 
 Greetings
 
 Heinz-Peter
 
 
 
-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Jan 29 2004