?
Solved

How do I create a sleep thread in C++.

Posted on 2006-05-23
10
Medium Priority
?
2,800 Views
Last Modified: 2008-01-09
I am looking at the following solution below and am looking for a detailed explaination of how the "WaitForSingleObject" function is working.  

Overall, I am looking to do something very similiar.  I need to create a timeout in a sepereate thread that if that timeout is reached I need it to exit a while loop.  If the while loop i am working with completes on its own, i would like to end the thread and continue.  If the length of the timeout is reached, I want to set a counter to mark that.

thanks for the help,
Rob


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
How do i write a Win32 Console application that creates one additional thread (i.e. it will consist of two threads).  The new thread will simulate working for a period of time by calling the system function Sleep to delay for a period of time of the order of 30 seconds (Sleep(30000)).  The main thread will monitor the child thread and output a dot to the screen each second until the child thread terminates.

Send to a Friend    Printer Friendly       
See Solution
            
      
Accepted Answer from _nn_
Date: 08/20/2003 10:32AM PDT
Grade: A
      Accepted Answer       

Something along these lines maybe :

#include <windows.h>
#include <stdio.h>

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    Sleep(30000);
    return 0;
}


int main( int argc, char *argv[] )
{
    DWORD dwThreadId;

    HANDLE hThread = CreateThread(
        NULL,       // pointer to security attributes
        0,          // initial thread stack size
        ThreadProc, // pointer to thread function
        0,          // argument for new thread
        0,          // creation flags (immediate)
        &dwThreadId // pointer to receive thread ID
    );

    if (NULL == hThread) {
        // error reporting here
        exit(1);
    }

    while(TRUE) {
        if (WaitForSingleObject(hThread, 1000)==WAIT_OBJECT_0)
            break;
        printf(".");
    }
    CloseHandle(hThread);
    printf( "\nDone !\n");
    return 0;
}


Comment from Tycotrix
Date: 08/20/2003 10:56AM PDT
      Author Comment       

Thanks a bunch ...it works perfectly well....thanks....
0
Comment
Question by:rspiege1
  • 6
  • 4
10 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 16745524
Hi rspiege1,
> f the length of the timeout is reached, I want to set a counter to mark that.

Do you mean you want to set a flag to mark?
I don't see the logic in setting up a counter, if the timer will act as a counter.

David Maisonave (Axter)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 16745551
rspiege1,
>  if (WaitForSingleObject(hThread, 1000)==WAIT_OBJECT_0)

For your reqirements, you should be checking for the value returned by WaitForSingleObject.
If it returns WAIT_TIMEOUT, then the time out has expired.
If it returns WAIT_OBJECT_0, then it has been signaled.
If it returns WAIT_ABANDONED, then something went wrong.



David Maisonave (Axter)
0
 
LVL 30

Expert Comment

by:Axter
ID: 16745579
FYI:
An object is signaled by calling SetEvent
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 16745598
controlEvent = CreateEvent (0, TRUE, FALSE,0);
......

 DWORD rc = WaitForSingleObject ( controlEvent, retrySleepTime);

...
... another thread
SetEvent( controlEvent );
0
 

Author Comment

by:rspiege1
ID: 16745759
i am looking to set a flag if the time set in the thread is actually reached.  Not a counter.  Sorry for the wrong term.

As for the value returned by "WaitForSingleObject", it takes two arguments.  What does the second argument refer to?  Is that how often it will check the thread?

If I wait for "WAIT_TIMEOUT" to occurr, what am I actually waiting for.  I assumed that the sleep in the thread was my timeout value.  Right now that is what I am using as my timeout value.

Also, how I can kill the thread prematurely?

thanks for all the help,
Rob
0
 
LVL 30

Expert Comment

by:Axter
ID: 16745823
>>What does the second argument refer to?  Is that how often it will check the thread?

That's how long it will wait to get signaled.  If it doesn't get signaled by the time it reaches the time out period, then the function will return WAIT_TIMEOUT.
>>If I wait for "WAIT_TIMEOUT" to occurr, what am I actually waiting for.

Usually, you're waiting for WAIT_OBJECT_0, and not WAIT_TIMEOUT.

WAIT_OBJECT_0 means that you got signaled.
WAIT_TIMEOUT means you did not get signaled, and you ran out of time.


>>I assumed that the sleep in the thread was my timeout value.  
That's not your timeout value.
That can cause a timeout value, but it's not a timeout value.
0
 

Author Comment

by:rspiege1
ID: 16745938
Thanks for your help Axter and your quick responses.  You have been a great help.

One more question, if I wanted to end the thread early before it reaches the sleep i set, is there a way of doing that?

thanks.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16746099
>>if I wanted to end the thread early before it reaches the sleep i set, is there a way of doing that?

Yes.  Call SetEvent.
SetEvent( controlEvent );
0
 

Author Comment

by:rspiege1
ID: 16746179
thanks again.  I just realized you already responded to this.

thanks,
Rob
0
 

Author Comment

by:rspiege1
ID: 16751611
Axter,

I noticed that after running SetEvent ( controlEvent);  the thread appears to still be running.  I'm assuming I must be typing something wrong.  If I run a "WaitForSingleObject() right after the SetEvent, I do not find a "WAIT_OBJECT_0"..

Does this make sense?

thanks,
Rob
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question