none
CreateThread in CE Driver appears to be single-threaded RRS feed

  • Question

  • I have a driver that creates a monitoring thread on Open using CreateThread.

    the monitor loop uses the kernel to fire an event on a given interrupt

    //something like this

    while(true) {

        WaitForSingleObject(isrEvt, INFINITE);

        if (success)

            InterruptDone();

    }

    My open function creates the thread and continues.

    But here is the problem:

    I want to replace the Interrupt by polling (I am reading a GPIO pin).

    The following code never runs as multi-threaded... on open, when the thread is created using CreateThread it never returns.

    //The following code runs on a new thread with low priority

    while (_running)

    {

        if (PinChangesValue())

        Count++;

    }

    but the CreateTHread never returns... it is spinning in the while loop.

    Q1) Why WaitForSingleObject allows the thread to run independent, but without it, the driver runs single-threaded?

    Thursday, February 23, 2012 12:13 AM

All replies

  • This is a case of processor bound process. This monopolises the processor.

    When the thread is created it continews to run. wherein "WaitForSingleObject" makes the thread as io bound, the event on which it is blocking is pushed to a doubly linked list "The wait queue" and the thread is blocked and runs when OS checks the status periodically.

    Hope this may help !

    --- Misbah


    Senior Design Engineer T.E.S Electroni Solutions (Bangalore-India) www.tes-dst.com email-misbah.khan@tes-dst.com

    Thursday, February 23, 2012 6:03 AM
  • Hi Misbah
    Thanks for your response.

    Q1) why/when does a thread monopolize the processor (io bound... what do you mean)? Is it because it is inside a user Dirver (kernel)?

    Q2) if I write a C++ app (not a driver) and I fire 3 different threads to count to 100 secs, I am pretty sure the 3 threads will finish almost at the same time and not sequentially staggered. Is CreateThread safe to use outside Drivers without WaitForObject?

    Q3) If I add a Sleep(1) inside my forever loop, is the driver multi-threaded again?




    Thursday, February 23, 2012 4:24 PM
  • 1. The original thread would block in WaitForSingleObject() until the event was signaled. That doesn't mean that it's constantly reading a location. It's actually not executing until the scheduler detects that, because the event is set, the thread can run. The new thread never blocks; it's always ready to run so it will use as much processor time as is available. Depending on the thread priority, it's possible that *no* other thread in the system will ever get a chance to run. Windows CE, because it is a real-time system, allows such monopolization of the processor; desktop Windows does not because it's more concerned with responding to the user.

    You can do what you've done, but it's wrong. You're assuring that your driver is going to be a bad citizen of the operating system. Why are you polling rather than using an interrupt? You need a *GOOD REASON* before deciding to change in this way. If you have a good reason, you need to find a better way to make the polling work.

    Paul T.

    Thursday, March 15, 2012 4:57 PM
  • Thanks Paul,

    I was fighting a stability issue and I wanted to remove the interrupts from the equation. To my surprise a forever loop without WaitForSingleObject() inside my driver monopolizes the processor.

    Your explanations are clear and I hope this conversation helps the developer community.

    Cheers

    Thursday, March 15, 2012 5:54 PM
  • Please find my reply as :

    Q1) why/when does a thread monopolize the processor (io bound... what do you mean)? Is it because it is inside a user Dirver (kernel)?

    Ans :- A thread running continuesly like (while(1)) and never blocks for an event/resource monopolizes the processor.  not related to user mode or kernel mode. Thread which blocks on an event and runs when signaled or blocks and runs are io bound thread.  

    Q2) if I write a C++ app (not a driver) and I fire 3 different threads to count to 100 secs, I am pretty sure the 3 threads will finish almost at the same time and not sequentially staggered. Is CreateThread safe to use outside Drivers without WaitForObject?

    Ans :- Yes, but if the 3 threads are blocking on same event. If on different events then it all depends on the signalling of the different events. When it is processor bound and all the threads have same priority level will complete together except an inversion has occurred at that instance. CreateThread can be used in all context without any exception and WaitForObject is for making the thread to block for an event or to make it to run periodically. 

    Q3) If I add a Sleep(1) inside my forever loop, is the driver multi-threaded again?

    Sleep(1) will block the thread to run every 1 ms ... Yes, multithreaded. But the thread is ready to run every 1 ms. Eg of waste of processor time. That is what is happening in your case without WaitForSingleObject()

    Hope i have answered all your questions !

    Reply if any thing is still not clear.

    --- Misbah


    Senior Design Engineer T.E.S Electroni Solutions (Bangalore-India) www.tes-dst.com email-misbah.khan@tes-dst.com

    Friday, March 16, 2012 6:08 AM