I'm pretty new to multi-threaded code, so I'm hoping that someone can help me with the problem I have.
I have a multi-part program comprised of an ONC/RPC server and other stuff (the "stuff" isn't really pertinent to my question, but it must be in the program with the server). Because svc_run() never returns I figured that I would put it in its own thread, and at program end I would simply terminate the thread and move on with life.
My program has now, however, expanded and I would like to cleanly, and safely, exit or close the ONC/RPC server, rather than terminate the thread. I cannot, however, figure out how to safely return from svc_run(). Can anyone help with this?
I've found a few other people with the same problem but no one seems to have responded to them. I have tried simply moving svc_run() into the same file as my server_process() function but the structures for fd_set don't get populated correctly (everything is 0), and the function fails.
svc_run() is defined in a dll created with the code found at: http://sourceforge.net/projects/oncrpc-windows/
I'm providing the pertinent elements of my code. Also note that svc_exit() does not seem to be a part of the onc/rpc system I'm currently using.
Sorry for the long question, Thank you,
Lex
Here's my code :
//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);
}
}
}