I'm writing an IOCP based server. For each connection to the server, I maintain a connection object that contains some context and state about that particular connection. When a client disconnects, I'd like to close the socket handle and free the connection object. However, a worker thread might free a connection object while another is accessing it. When can I be sure that the object is safe to free? I considered reference counting or shared smart pointers, but it needs to be thread safe as well. Thank you for your consideration towards this problem.
IOCP question.
Collapse
X
-
Tags: None
-
There is a reference counted smart point in this article.
You would need to chnage the places where the reference count is changed to not use the C++ increment/decrment operators but instead call the functions your OS provides for interlocked increment/decrement.
Only the OS can provide a thread-safe reference count. C++ just generates machine code and that's never thread-safe.
Comment