CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    83

    WaitForSingleObject, How to stop a thread?

    I thought that WaitForSingleObject would return immediately for a thread which is stoped, but in the code below the WaitForSingleObject seems to wait infinitely. Why ?
    Code:
    volatile bool stop;
    
    DWORD WINAPI SampleThread(LPVOID iValue)
    {
    	while(!stop) {
    		Sleep(500);
    	}
    	return 0;
    }
    
    
    int WINAPI WinMain(	HINSTANCE hInstance,
    					HINSTANCE hPrevInstance,
    					LPTSTR    lpCmdLine,
    					int       nCmdShow)
    {
    	stop=false;
    	HANDLE hThread;
    	DWORD dwGenericThread;
    	hThread = CreateThread(NULL,0,SampleThread,NULL,0,&dwGenericThread);
    	if(hThread == NULL)
    	{
    		DWORD dwError = GetLastError();
    		return;
    	}
    	stop=true;
    	WaitForSingleObject(hThread,INFINITE);
    	return;
    }

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: WaitForSingleObject, How to stop a thread?

    What is the waitforsingleobject doing ??

    Code:
    // thread is stopping here. . 
    stop=true;
    
    // what are you waiting for ??
    WaitForSingleObject(hThread,INFINITE);

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    Re: WaitForSingleObject, How to stop a thread?

    Skizmo's got your number here.


    I can see you're testing theory of design here, the stop flag adjustment might seem like a good test, but Skizmo's identified a flaw.

    It's entirely possible that the thread object is GONE before you wait.

    Either signal the stop from some other thread, or...start your thread suspended.

    Or, consider using an event.

    The whole design goal is to wait on thread completion, but there are other concepts in threads you might wait on that will imply an event anyway.

    It's better, most of the time, to create a thread that waits on an event so that you can fill a queue with 'jobs' for it to run. This means you'd have an event anyway.

    You could construct this wait on an autoreset event. If you had, and the exit from the thread process had signaled that event, you wouldn't have this bug.

    If you prefer to wait on the thread, you have to be certain it's not possible, even if unlikely, that the thread could have completed before you entered the wait.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: WaitForSingleObject, How to stop a thread?

    Quote Originally Posted by Divad
    I thought that WaitForSingleObject would return immediately for a thread which is stoped, but in the code below the WaitForSingleObject seems to wait infinitely. Why ?
    Well, man, your code indeed returns immediately. So your thought was absolutely correct.
    Best regards,
    Igor

  5. #5

    Re: WaitForSingleObject, How to stop a thread?

    Your code is correct. even the thread completed before you wait, it will still returns immediately.
    Best Api Monitor tool.
    Trace the target program automatically and monitor the parameters of all API and COM interfaces.

    Auto Debug for Windows 4.0
    Auto Debug for .Net
    http://www.autodebug.com/

  6. #6
    Join Date
    Nov 2006
    Posts
    83

    Re: WaitForSingleObject, How to stop a thread?

    My example actually work. The problem was my debugger. When I stepwise went through my program, it halt infinately.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured