Note that there are some explanatory texts on larger screens.

plurals
  1. POIOCP multithreaded server and reference counted class
    primarykey
    data
    text
    <p>I work on IOCP Server (Overlapped I/O , 4 threads, CreateIoCompletionPort, GetQueuedCompletionStatus, WSASend etc). And my goal is to send single reference counted buffer too all connected sockets.(I followed Len Holgate's suggestion from this post <a href="https://stackoverflow.com/questions/19760522/wsasend-to-all-connected-socket-in-multithreaded-iocp-sever">WSAsend to all connected socket in multithreaded iocp server</a>) . After sending buffer to all connected clients it should be deleted.</p> <p>this is class with buffer to be send</p> <pre><code>class refbuf { private: int m_nLength; int m_wsk; char *m_pnData; // buffer to send mutable int mRefCount; public: ... void grab() const { ++mRefCount; } void release() const { if(mRefCount &gt; 0); --mRefCount; if(mRefCount == 0) {delete (refbuf *)this;} } ... char* bufadr() { return m_pnData;} }; </code></pre> <p>sending buffer to all socket</p> <pre><code>refbuf *refb = new refbuf(4); ... EnterCriticalSection(&amp;g_CriticalSection); pTmp1 = g_pCtxtList; // start of linked list with sockets while( pTmp1 ) { pTmp2 = pTmp1-&gt;pCtxtBack; ovl=TakeOvl(); // ovl -struct containing WSAOVERLAPPED ovl-&gt;wsabuf.buf=refb-&gt;bufadr();// adress m_pnData from refbuf ovl-&gt;rcb=refb; //when GQCS get notification rcb is used to decrease mRefCount ovl-&gt;wsabuf.len=4; refb-&gt;grab(); // mRefCount ++ WSASend(pTmp1-&gt;Socket, &amp;(ovl-&gt;wsabuf),1,&amp;dwSendNumBytes,0,&amp;(ovl-&gt;Overlapped), NULL); pTmp1 = pTmp2; } LeaveCriticalSection(&amp;g_CriticalSection); </code></pre> <p>and 1 of 4 threads</p> <pre><code> GetQueuedCompletionStatus(hIOCP, &amp;dwIoSize,(PDWORD_PTR)&amp;lpPerSocketContext, (LPOVERLAPPED *)&amp;lpOverlapped, INFINITE); ... lpIOContext = (PPER_IO_CONTEXT)lpOverlapped; lpIOContext-&gt;rcb-&gt;release(); //mRefCount --,if mRefCount reach 0, delete object </code></pre> <p>i check this with 5 connected clients and it seems to work. When GQCS receives all notifaction, mRefCount reachs 0 and delete is executed.</p> <p>And my questions: is that approach appropriate? What if there will be for example 100 or more clients? Is situation avoided when one thread can delete object before another still use it? How to implement atomic reference count in this scernario? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload