Re: CAsyncSocket Close()
On Oct 12, 8:03 pm, mfc <mfcp...@googlemail.com> wrote:
using the CAsyncSocket example from Mr. Newcomer (http://www.flounder.com=
/detach.htm#Sockets) as a starting point - I`m facing
maybe one synchronisation problem.
I want to close the socket (class SocketThread :public CWinThread)
during the software is running by clicking on a button (for example).
Is it still enough to write only a PostThreadMessage from the
mainthread to the SocketThread (m_myThread):
::PostThreadMessage(m_myThread->m_nThreadID, UWM_TERM_THREAD, 0, 0);
to kill the socket thread as well as all http connections which were
established at this moment? These http connections threads will be
established in the OnAccept() method of the class CConnSoc :public
For example if one http connection thread needs some values from the
mainthread I `ve installed a PostMessage with a std:map to the
mainthread asking for these values and getting back by another
PostMessage to the http connection thread.
std::map<CString, CString> map = new (std::map<CString, CString>)
wnd->PostMessage(UWM_TO_MAINTHREAD, map, threadID);
// anyhting else
If this http connection thread will be closed or killed by clicking on
the button - I`ll have some memory leaks if the http connection thread
has send a UWM_TO_MAINTHREAD to mainthread but can`t return to this
thread (GetMsgBackFromMainthread) to delete the map...
Doesn't Joe's example show what to do to retrieve messages from main
thread's queue before destroying whoever it is that is handling them?
If not, or if it's not possible to create cleanup function that does
that, you can always rely on shared memory. Put your data (map) in a
place shared between two threads, use PostMessage to tell main thread
that there is something to process, but take data out of said shared
place, and clean it up properly when done.
E.g (disclaimer: compiled with head-compiler and tested with head-
typedef std::auto_ptr< std::map<CString, CString> > PData;
PData pdata(new (std::map<CString, CString>));
where wnd is an object having e.g.
class CMyWnd : public CWnd
CArray<std::map<CString, CString> *> m_sharedData;
void PostDataToMainThread(PData pdata)
CSingleLock l(&m_cs, TRUE);
CSingleLock l(&m_cs, TRUE);
LRESULT OnUWmSomething(LPARAM, WPARAM)
delete objects still in m_sharedData
alternatively, process them^^^
(remember to be exception-free here in the destructor).
Note that, due to async nature of PostMessage, you can fail to process
some messages (that seems to happen now, too, and you see this as
memory leaks). If it's important to process all, then do it
I am using auto_ptr to mark ownership transfer of heap objects and be
exception-safe. E.g. when you enter PostDataToMainThread, PData param
takes ownership. CMyWnd takes over. Similar for GetData() etc. You can
use shared_ptr for that, and then you can put said shared_ptr into the
array (can't do that with auto_ptr). But shared_ptr has overhead and
you need boost or new, not yet finalized, standard library (well, new C
+ standard is not finalized yet, I think).