我目前正在尝试使用套接字在 C++ 中实现服务器。我试图通过阻止 SIGINT 信号来防止竞争条件,直到它卡在阻塞 pselect 中。从那里,它应该退出,更改我的循环变量,然后退出线程。从我试图让它工作的尝试来看,它似乎到达了 pselect(),但使用我的代码并没有被中断。任何帮助表示赞赏。
监听器.h:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
class CListener
{
public:
CListener();
void quitListener(void);
private:
void* InitListener(void);
static void* StartListenerThread(void* context);
static bool mbListening;
pthread_t mtThreadID;
};
监听器.cpp
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#include "Listener.h"
bool CListener::mbListening = true;
CListener::CListener()
{
mbListening = true;
mtThreadID = 0;
pthread_create(&mtThreadID, NULL, &CListener::StartListenerThread, this);
}
void* CListener::StartListenerThread(void* context)
{
return ((CListener*)context)->InitListener();
}
void* CListener::InitListener()
{
sigset_t tSignalSet;
sigset_t tOriginalSignalSet;
sigemptyset(&tSignalSet);
sigaddset(&tSignalSet, SIGINT);
sigprocmask(SIG_BLOCK, &tSignalSet, &tOriginalSignalSet);
FD_ZERO(&tConnectionSet);
FD_SET(0, &tConnectionSet);
while(mbListening)
{
tSelectSet = tConnectionSet;
std::cout << "Reached pselect\n";
nReadyConnections = pselect(nSelectSocket+1, &tSelectSet,
NULL, NULL, NULL, &tOriginalSignalSet);
std::cout << "Broke out of pselect\n";
if(nReadyConnections < 0 && errno == EINTR)
{
mbListening = false;
}
}
pthread_exit(NULL);
return NULL;
}
void CListener::quitListener()
{
raise(SIGINT);
}
只要我正确地复制了所有内容,您就应该能够运行:
CListener tListener = CListener();
usleep(20000);
tListener.quitListener();
并且输出应显示在终端中。我的最终目标是我可以允许 pselect 被中断,而不会破坏任何可能发生的处理并允许线程优雅地关闭。(阻塞在 pselect > 接收 SIGINT > 中断 pselect > 返回循环 > 完成并退出)