Thread: multithreaded programming -- strange behavior

  1. #1
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96

    multithreaded programming -- strange behavior

    I'm working on a program, but get stuck up. When I debug, it "sometimes" totally freezes the debugger. I removed everything possible while preserving the problem reproducable. Smallest code doing the same thing is here:
    Code:
    #include <windows.h>
    
    DWORD WINAPI FileopenThread(PVOID pParam) {
    	// breakpoint on the following line
    	return 0;
    }
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE h, PSTR szCmdLine, INT nCmdShow) {
    	MSG msg;
    	HWND hWnd;
    	HANDLE hThread;
    	WNDCLASSEX wcex = {sizeof(WNDCLASSEX), 0, (WNDPROC)DefWindowProc, 0, 0, hInstance, 0, 0, 0, 0, "MyClass", 0};
    	RegisterClassEx(&wcex);
    	hWnd = CreateWindowEx(0, "MyClass", "title", 0, 0, 0, 0, 0, 0, 0, hInstance, 0);
    	ShowWindow(hWnd, nCmdShow), UpdateWindow(hWnd);
    	// breakpoint on the following line
    	hThread = CreateThread(0, 0, FileopenThread, "datafile", 0, 0);
    	CloseHandle(hThread);
    	while (GetMessage(&msg, 0, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    Compile it in Visual C++ 6 (I also have Service Pack 5) and run debugger. It stops on the lower breakpoint. Press Go (F5) and if it now stops on the higher breakpoint, stop debugging (Shift + F5) and repeat the process (running debugger and so on..) Usually in about three, five times it freezes. I'm desperate, chasing such bug is a torture. Thanks in advance for any help...
    Last edited by larry; 09-15-2004 at 12:59 AM.
    Please excuse my poor english...

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I don't have much experience in debugging multithreaded applications. I'm just wondering why you have commented away the call to CloseHandle().
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There is nothing technically wrong with the code you've posted except that the thread's handle should be closed and some point (usually when the thread has finished running).

    I can run it through the two break points all day long on my Win2K machine with the same VC++ environment.

    gg

  4. #4
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    I'm just wondering why you have commented away the call to CloseHandle().
    I removed the comment. It seems the problem is the same.

    I can run it through the two break points all day long on my Win2K machine with the same VC++ environment.
    I tried it now and it freezed right in the first attempt (on WinXP). It doesn't freeze forever, but it takes about 20 seconds to get to the second breakpoint. But, as I said, usually it runs perfectly.
    Please excuse my poor english...

  5. #5
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    I experienced a lot of things in programming, but this wouldn't even come to my mind. The problem mentioned never occured again since I:

    switched to full rights Windows XP user profile

    Believe it or not.

    Thanks for help anyway, it helped me that I knew the code was correct.
    Please excuse my poor english...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  3. C++ list Strange Behavior
    By yongzai in forum C++ Programming
    Replies: 19
    Last Post: 12-29-2006, 02:56 AM
  4. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM
  5. Strange behavior with CDateTimeCtrl
    By DonFiasco in forum Windows Programming
    Replies: 2
    Last Post: 12-19-2004, 02:54 PM