-
December 21st, 2009, 03:33 AM
#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;
}
-
December 21st, 2009, 04:14 AM
#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.
-
December 21st, 2009, 05:30 AM
#3
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
-
December 21st, 2009, 05:33 AM
#4
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
-
December 21st, 2009, 07:03 AM
#5
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
-
December 24th, 2009, 01:47 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|