Completion Port and Multithreads :: MFC

Started by
5 comments, last by kuphryn 21 years, 5 months ago
Hi. I am working under an MFC environment. I would like to know, What is the performance difference, if any, resulting from the use of _beginthreadex and MFC''s AfxBeginThread for an IOCP Winsock model client and server? I understand that for an MFC project, we should use MFC''s AfxBeginThread to generate the threads. Nonetheless, does an IOCP client or server require or performs better via _beginthreadex? The reason I am wondering about this is that I saw some really good examples IOCP servers where the developers used _beginthreadex with MFC instead of AfxBeginThread(). Here is one example. http://www.codeproject.com/internet/winsockiocp.asp ----- A second question related to IOCP, _beginthreadex, and MFC''s AfxBeginThread is the callback function. Under MFC''s AfxBeginThread, you basically send/post messages from the worker thread the IOCP GetQueuedCompletionStatus() function returns. One the other hand, with _beginthreadex, you have the option of passing in a pointer to the class object itself (this). Thus, you can call a function to process incoming data directly without relying on send/post message. I would like to know, Is the analysis above true. In other words, does the use of send/post message via IOCP worker threads negatively affect performance? Thanks, Kuphryn
Advertisement
honestly, i didn''t understand the entire post, namely how exactly you think afx-started threads interact with other threads. however, one of AfxBeginThread overloads has the semantics of CreateThread/_beginthread[ex], which as i understand is the one you are supposed to use for worker threads. the difference between it and win32/crt versions is only the startup and cleanup code involved. once your thread is running, it doesn''t matter who created it: it will function in the same way regardless of startup code.
Okay. Thanks.

The first questions really asks is that from what I read in Jeff Prosise''s book on MFC we should use AfxBeginThread for creating a worker thread when working in an MFC environment. I want to know if there is any performance issues when using _beginthreadex instead. Futhermore, how about stability?

You answered the issue of performance.

Kuphryn
to answer the question on stability, i''ll have to open mfc docs, which i can''t be bothered to do. if you aren''t using any mfc from the worker thread, _beginthread will work just fine. if you are, well, read the docs yourself or wait for someone else to read them. i suspect there might be some per-thread afx state management wizardry involved.
Here is an interesting point about _beginthreadex(). _beginthreadex() and AfxBeginThread() take either a global callback function or a static class member function. Is there an advantage one has over the other?

Secondly, I pass in the threads a global structure that holds pointers to data I want the threads to service. If the callback function is a static member function, is it still possible to make the global function a private data member, i.e. declare the structure as in private section?

Kuphryn
1) differences are only cosmetic.

2) if you want to get to member data, pass this pointer as lpvParam to either *begin* function, and call a member function from your global/static threadproc to do the work.
Okay. Thanks.

Kuphryn

This topic is closed to new replies.

Advertisement