-
May 17th, 2010, 04:43 AM
#1
Posting messages between threads
Hi, I post my problem in Slow Chat thread http://www.codeguru.com/forum/showthread.php?t=496443. Thank you for all your answers. As MikeAThon wrote - that I can't block InitInstance function of CWinThread class. So my question is - where should I put my mainLoop() function in my custom thread? OnIdle ? And one more question: I tried to add CList object to my thread as a message list. From main thread I'm adding new messages to that list. And in my thread I would like to get the message objects from that list. I surrounded all operation on that list with CriticalSection. But it's not working - some heap errors appears. is it possible to share CList objects between threads, in one thread create objects by new operator and add it to list, and in another thread gets it from list and deleteing by delete ?
Code:
void CMessageQueue::sendMessage( MessageStruct *mess ) {
if ( mess != NULL ) {
EnterCriticalSection( &critSection );
queue.AddTail( mess );
LeaveCriticalSection( &critSection );
}
}
//////////////////////////////////////////////////////////////////////////
MessageStruct *CMessageQueue::receiveMessage() {
MessageStruct *mes = NULL;
if ( getMessageCount() > 0 ) {
EnterCriticalSection( &critSection );
mes = queue.RemoveTail();
LeaveCriticalSection( &critSection );
}
return mes;
}
//////////////////////////////////////////////////////////////////////////
int CMessageQueue::getMessageCount() {
int count = 0x00;
EnterCriticalSection( &critSection );
count = queue.GetCount();
LeaveCriticalSection( &critSection );
return count;
}
-
May 17th, 2010, 10:06 AM
#2
Re: Posting messages between threads
>> So my question is - where should I put my mainLoop() function in my custom thread?
It looks like you're attempting to implement a "user interface thread", but you may only need a "worker thread". Read more about it here: http://msdn.microsoft.com/en-us/library/975t8ks0.aspx
>> But it's not working - some heap errors appears.
One error I see is in receiveMessage() - once getMessageCount() returns, the count is meaningless. You need to get the count and call RemoveTail() while inside the critical section.
gg
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
|