[英]Clean Exit of ONC RPC SVC_RUN() with MFC
我對多線程代碼很陌生,所以我希望有人可以幫助我解決我遇到的問題。
我有一個由 ONC/RPC 服務器和其他東西組成的多部分程序(“東西”與我的問題並不真正相關,但它必須在服務器程序中)。 因為 svc_run() 永遠不會返回,所以我想我會將它放在它自己的線程中,並且在程序結束時我會簡單地終止線程並繼續生活。
然而,我的程序現在已經擴展,我想干凈、安全地退出或關閉 ONC/RPC 服務器,而不是終止線程。 但是,我無法弄清楚如何安全地從 svc_run() 返回。 有人能幫忙嗎?
我發現其他幾個人有同樣的問題,但似乎沒有人回應他們。 我嘗試簡單地將 svc_run() 移動到與 server_process() function 相同的文件中,但 fd_set 的結構沒有正確填充(全部為 0),並且 function 失敗。
svc_run() 在 dll 中定義,代碼創建於: http://sourceforge.net/projects/oncrpc-windows/
我正在提供我的代碼的相關元素。 另請注意,svc_exit() 似乎不是我當前使用的 onc/rpc 系統的一部分。
抱歉問了這么長的問題,謝謝,
萊克斯
這是我的代碼:
//Code in Initialization of MFC Dialog
myThread = AfxBeginThread(startServing,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL);
// Thread that starts the server
UINT __cdecl startServing(LPVOID pParam)
{
if(server_process())
AfxMessageBox(_T("Error Starting the VXI 11 Server."));
AfxEndThread(0,TRUE); //I never get here.
return 0;
}
//How I would like to stop the thread:
void myDlg::OnBnClickedQuit()
{
DWORD threadStatus;
endThread = 1; //static or extern that could be monitored if svc_run()
//wasn't in a dll
threadStatus = WaitForSingleObject(myThread->m_hThread, INFINITE);
OnOk(); //for the ok modal to close the progam
}
//How I end up stopping my thread
void myDlg::OnBnClickedQuit()
{
TerminateThread(myThread->m_hThread,1);
OnOk(); //for the ok modal to close the progam
}
int server_process()
{
//Portmap and server registration code
.
.
.
.
svc_run(); //ideally I'd be able to monitor a global in here
(void)fprintf(stderr, "svc_run returned\n");
#ifdef WIN32
rpc_nt_exit();
#endif
return(1);
}// end of server Process
// Function in the dll I'm calling
void svc_run()
{
#ifdef FD_SETSIZE
fd_set readfds;
#else
int readfds;
#endif /* def FD_SETSIZE */
#ifndef WIN32
extern int errno;
#endif
for (;;) {
#ifdef FD_SETSIZE
readfds = svc_fdset;
#else
readfds = svc_fds;
#endif /* def FD_SETSIZE */
#ifdef WIN32
switch (select(0 /* unused in winsock */, &readfds, NULL, NULL,
#else
switch (select(_rpc_dtablesize(), &readfds, (int *)0, (int *)0,
#endif
(struct timeval *)0)) {
case -1:
#ifdef WIN32
if (WSAerrno == EINTR) {
#else
if (errno == EINTR) {
#endif
continue;
}
perror("svc_run: - select failed");
return;
case 0:
continue;
default:
svc_getreqset(&readfds);
}
}
}
嗯,答案就在你的問題中。 您必須編寫自己的svc_run()
並使用條件更改無限循環:
while(!done) {
select(....)
svc_getreqset(&readfds);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.