使用MFC清除ONC RPC SVC_RUN()的退出

时间:2011-05-26 15:52:18

标签: multithreading rpc sunrpc

我对多线程代码很陌生,所以我希望有人可以帮我解决我遇到的问题。

我有一个由ONC / RPC服务器和其他东西组成的多部分程序(“东西”与我的问题并不相关,但它必须在服务器的程序中)。因为svc_run()永远不会返回,我认为我会把它放在自己的线程中,而在程序结束时我会简单地终止线程并继续生活。

我的程序现在已经扩展了,我想干净,安全地退出或关闭ONC / RPC服务器,而不是终止线程。但是,我无法弄清楚如何安全地从svc_run()返回。有人能帮忙吗?

我发现其他一些人有同样的问题,但似乎没有人回应他们。我试过简单地将svc_run()移动到与我的server_process()函数相同的文件中,但是fd_set的结构没有正确填充(一切都是0),并且函数失败。

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);
       }
   }
}

1 个答案:

答案 0 :(得分:0)

嗯,答案就在你的问题中。您必须编写自己的svc_run()并使用条件更改无限循环:

while(!done) {
  select(....)
  svc_getreqset(&readfds);
}