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

    thread synchronization problem

    Hello

    i use 1 thread in mysource for get data,... ,but sometimes i get wrong data
    it is more when i twiddle with my software windows(i mean compile file)

    i think this is a synchronization problem

    how fix this problem ?

    thnx for my help!


    somecode i use
    Code:
    void _stdcall dll_callback(unsigend char*d ,int l)
    {
       if(d[0]==0x19){
           PostThreadMessage( m_ThreadId ,DATA_MESSAGE_1 ,(WPARAM)d ,l);
    
       }
    
       if(d[1]==0x20){
          PostThreadMessage( m_ThreadId ,DATA_MESSAGE_2 ,(WPARAM)d  ,l);
       }
    }
    
    
    DWORD Receive_Data(/* int id */)
    {
        CSingleLock Lock(&c_s);
        
        while( 1 )
        {
              MSG  msg;
              if(GetMessage(&msg ,NULL ,0 ,0 ))
    		  {
                      Lock.Lock();
    			unsigned char *BUF=(unsigned char *)msg.wParam;
    			switch(msg.message)
    			{
    			  case DATA_MESSAGE_1 :
    				  dump("Data1:" ,BUF ,msg.lParam);
    			  break;
    
    
    			  case DATA_MESSAGE_2 :
    				  dump("Data2:" ,BUF ,msg.lParam);
    			  break;
    			}
                     Lock.Unlock();
    		}
        }
        return 0;
    }
    
    int Thread_Init()
    {
    
    m_hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Receive_Data ,(void*)0 ,NULL ,&m_ThreadId;
    if(!m_hThread){
    log("!CreateThread\n");
    	 return false ;
    }
    
    return true;
    }

  2. #2

    Re: thread synchronization problem

    Remember a lock's not useful if just one thing uses it. The receive data looks ok, but if the buffer is shared between threads both sides must use the lock, if not, it's not needed.

  3. #3
    Join Date
    Nov 2004
    Posts
    133

    Re: thread synchronization problem

    Hi

    since you are using a message queue , and you are posting message to the thread, i dont think there is any further need to use the CSingleLock, as the messages are queued, they are natually synchoronized.



    regards
    pradish

  4. #4
    Join Date
    Nov 2004
    Posts
    133

    Re: thread synchronization problem

    hi


    since you are using a message queue , and you are posting message to the thread, i don’t think there is any further need to use the CSingleLock, as the messages are queued, they are naturally synchronized.


    regards
    pradish

  5. #5
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: thread synchronization problem

    Hi,

    PostThreadMessage will return without waiting for the message to be read.

    So, if the content pointed to by 'd' changes before it is read by the thread, you'll read the wrong data.

    There isn't any equivalent to SendMessage (which blocks until the message is received) for threads. SendMessage sends a message to a window by calling the handler directly, without going through the message pump, but this isn't possible if you are sending to a thread.

    So, you do need to find some way of synchronising it, or make sure that the data can't change (maybe you can make it static.)

    Alan

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: thread synchronization problem

    Prefer _beginthreadex over CreateThread as CreateThread doesn't always do the right thing if you are using the crt library.

    With regard to threading, I generally avoid passing data between threads via the Windows messaging subsystem (i.e PostThreadMessage). I don't like the fact that any receiving threads needs to have a message loop to process the data. The exception to this is in the UI thread, where I'll have worker threads post 'notification' messages to the UI thread to inform it that data or a specific event has occurred.

    If I need to share data between threads, then I typically use a stl collection that I make thread safe and share the collection between the threads.

    For a few samples of this approach, read the articles listed in my signature line.

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