CSocket and Multithread
Nick Shanny -- nshanny@msn.fullfeed.com
Tuesday, January 23, 1996
Hello,
I have a few questions concerning CSocket and MFC4.0. It seems MFC 4.0 uses thread
local storage for just about everything including CWnd maps.
If I create a server CSocket, issue a listen and then an accept, the connected CSocket
returned will have an invisible window attached to it by virtue of CAsyncSocket. If I
then spawn another thread to handle the connection, the thread created will not have
that mapping by virtue of TLS. Therefore, asserts will fly. Is this logic correct? If
so, how does one use the standard notion of a server spawing threads for each
connection and use CSocket?
Thanks,
Nick Shanny
nshanny@fullfeed.com
Les -- Les.Hill@FMR.Com
Friday, January 26, 1996
I did this recently and the solution is to do something along the lines of
(this is from memory as both VC4 and my source are at home :(
start server socket
in overriden OnAccept *do not call* accept member function
call ::accept(...)
set up new thread (I use a derived class for this) with the new socket value
start new thread
in overridden init-thread-function (name escapes me) use the attach
feature of CAsyncSocket to latch onto the new socket value
go into message loop (this thread then does its work)
go back to listening state
To do this you need at least three derived classes:
A CAsyncSocket derived class for the server socket
A CAsyncSocket derived class for the working socket
A CWinThread derived class for the working socket's thread,
which has both the new SOCKET and the working socket CAsyncSocket
class as members
It is actually fairly easy to do.
Les
----------
Hello,
I have a few questions concerning CSocket and MFC4.0. It seems MFC 4.0 uses
thread
local storage for just about everything including CWnd maps.
If I create a server CSocket, issue a listen and then an accept, the
connected CSocket
returned will have an invisible window attached to it by virtue of
CAsyncSocket. If I
then spawn another thread to handle the connection, the thread created will
not have
that mapping by virtue of TLS. Therefore, asserts will fly. Is this logic
correct? If
so, how does one use the standard notion of a server spawing threads for
each
connection and use CSocket?
Thanks,
Nick Shanny
nshanny@fullfeed.com
| Вернуться в корень Архива
|