C语言socket服务端,接收客户端报文,使用新线程处理客户端报文。
处理数据线程,有如下代码。
pthread_mutex_lock(&logfile_lock);
if (NULL == g_logfile_fp || MAX_LOG_NUM < g_logfile_size_counter)
...
g_timet0 = timet_now;
g_logfile_size_counter++;
pthread_mutex_unlock(&logfile_lock);
gdb attach 服务端进程号,bt发现所有线程都卡到了pthread_mutex_lock(&logfile_lock);
可以确定,服务端卡住,是因为有线程锁住logfile_lock。
查找原因,发现程序有时钟信号程序如下
double difference = difftime(timet_now, pstCliInfo->HeartBeat_FinalTimet);
if (0 < difference - CLINET_HEARTBEAT_TIMEOUT*60)
{
n++;
printf_log("client @ socket %d tid %lu heartbeat timeout\n",
pstCliInfo->clientID, pstCliInfo->tid);
Close(pstCliInfo->clientID); /* done with connected socket */
Pthread_cancel(pstCliInfo->tid); /* subthread over */
/* clear client info */
}
每隔一段时间,判断一次客户端,发送报文的最后时间,超时,则关闭这个线程。
出现锁住资源未释放的时候,线程被关闭。导致服务端一直卡住。程序不能正常运行。