From: Daniel M. <da...@im...> - 2005-04-05 20:28:46
|
"Luke Dunstan" <cod...@ho...> wrote in news:BAY...@ph...l: > > "-mthreads" is the equivalent switch, but I doubt it will make a > difference because it really only affects C++ exception handling. > Could you provide a small but complete example that demonstrates the > problem? > > I think you also need to use _beginthread() instead of CreateThread(). > > Luke > Okay, at the risk of posting too large of a message, I'm enclosing a stripped-down version of my program. It deletes all the graphics-drawing code, but includes everything else. I also switch to _beginthread() after reading the notes in msdn, but that has had no effect on program operation. Again, if I compile under VC6 with: cl /W3 /O2 /G4 /MT rainbowt.cpp user32.lib gdi32.lib this program works fine. If I compile under MinGW 3.4.2 with: g++ -Wall -s -O3 -mthreads rainbowt.cpp -o rainbowt.exe -lgdi32 the program runs, the main window is drawn, but no output ever appears. However, if I uncomment the file-printing debug code at the end of this code, then the g++ application runs just fine!!! Even eliminating the fprintf statement, however, puts me back to nothing drawing. I have *no* idea what fprintf has to do with any of this... I had included it just to find out whether or not my main thread was even being executed... Dan #include <windows.h> #include <stdlib.h> #include <math.h> #include <process.h> //*********************************************************************** LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int cxClient = 0 ; int cyClient = 0 ; static void display_current_operation(HWND hwnd); static VOID Thread (PVOID pvoid); HWND hwnd ; //*********************************************************************** int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg ; WNDCLASSEX wndclass ; wndclass.cbSize = sizeof (wndclass) ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ; RegisterClassEx (&wndclass) ; hwnd = CreateWindow (szAppName, szAppDesc, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } //*********************************************************************** LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_CREATE: cxClient = 0 ; cyClient = 0 ; _beginthread(Thread, 0, NULL) ; return 0 ; case WM_SIZE: cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; } //***************************************************************** // this function plots one primary and one secondary pixel //***************************************************************** void display_current_operation(HWND hwnd) { if (cxClient == 0 || cyClient == 0) return ; //************************************* // draw graphics operation shere //************************************* } //***************************************************************** // separate thread for drawing function //***************************************************************** // #include <stdio.h> VOID Thread (PVOID pvoid) { // If I don't have the fprintf statement inside the // while loop, the function does not execute!! // unsigned loops = 0 ; // FILE *fd = fopen("\\dbg.txt", "wt") ; while (TRUE) { display_current_operation(hwnd); // fprintf(fd, "%u,", loops++) ; } // fclose(fd) ; } |