POSIX threads problem, probably simple fix

This is a discussion on POSIX threads problem, probably simple fix within the C++ Programming forums, part of the General Programming Boards category; I have a function that checks for messages within the program like so: Code: void *MessageCheck(void *ptr); void *MessageCheck(void *threadID) ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    POSIX threads problem, probably simple fix

    I have a function that checks for messages within the program like so:

    Code:
    void *MessageCheck(void *ptr);
    
    void *MessageCheck(void *threadID)
    {
            printf("Begin!\n");
    	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    	{
    		if (msg.message==WM_QUIT)
    		{
    			printf("Exit?\n");
    			done=TRUE;
    		}
    		else
    		{
    			TranslateMessage(&msg);				// Translate The Message
    			DispatchMessage(&msg);				// Dispatch The Message
    		}
    	}
    }
    and when I try to initialize this thread using:

    Code:
    pthread_create(&threads[0], NULL, MessageCheck, (void *)0);
    pthread_join(threads[0], NULL);
    the printf("Begin!\n") keeps looping, which it is supposed to, but absolutely no messages are noticed, no matter what i do, keyboard input, mouse nothing and on top of that the program freezes... I am running this in an infinite loop like so:

    Code:
    while(done==FALSE)
    {
    	pthread_create(&threads[0], NULL, MessageCheck, (void *)0);
    	pthread_join(threads[0], NULL);
    }
    and what I think should be happening is that it will keep running until it receives a quit message then exit the while loop, but when I try to quit it wont acknowledge that I even sent a message...

    Im really confused right now and would like some help

    The reason I am confused is because it seems to be calling the thread fine, and executing things normally, but it just seems to skip everything that deals with the loops, and I am also having problems with another thread writing to the screen with OpenGL, but thats for after this is fixed, hopefully they will fix eachother
    Last edited by parad0x13; 07-23-2008 at 04:30 PM.

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Hum, after looking briefly at PeekMessage() documentation on MSDN, I saw this:

    hWnd
    [in] Handle to the window whose messages are to be retrieved. The window must belong to the current thread.

    If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.
    So, instead of passing NULL, you might want to pass a real window handle. But that wouldn't work since the window handle doesn't belong to the current thread. So I don't really know, but your problem might be there. But note that have close to no experience in using the windows user interface functions.
    I hate real numbers.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    Each Win32 message Q is associated with exactly one thread, and vice-versa . When a thread creates a window, that window is associated with that thread's Q. That window will only receive messages that are "pumped" within the owning thread.

    Typically you have only one UI thread - which creates all the windows and pumps all the messages. Any additional threads would be worker threads that don't interact with the UI directly.

    gg

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    Well that works out well because I want one thread to blit to screen, and the other to watch for user interaction messages. If a user interaction, like keyboard hit, is detected I want the first loop, bliting function, to stop.

    I know blitting takes like no time at all, but what Im doing is replacing a very complex mathematical process that takes a long time to compute. So if I can get this to work I can hopefully get that to work.

    My goal is to have 3 threads.
    The thread for math and blitting
    A thread for message scanning
    The main program thread that waits for those two threads to complete before continuing

    If I can get that accomplished I'd be a very happy dude,...

    But I cant even seem to get either function to properly execute when either one of them is ran as a thread... So my question lies within the how-to of creating a properly executing thread that executes either of the 2 math/message threads

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with POSIX sockets
    By MCRonald in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 10:41 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  5. problem using threads......?
    By stumpert in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2002, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21