我有一个服务器,每次客户端连接到服务器时都会创建一个分离的线程,并且该代码的要点在这里: “
tid_data.accept_skt = accept_skt;
tid_data.slot = slot;
snprintf(tid_data.client_info, sizeof(tid_data.client_info),
"%s:%d\n", ipstr, port);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&nextThread, &attr, credsChckrThread, (void*)&tid_data) != 0) {
CSprintf("Error: pthread_create() failed - %s", strerror(errno));
close(accept_skt);
continue;
}
” 这是线程函数(省略它调用的函数) “
static void *credsChckrThread(void *data)
{
tid_data_t tid_data;
int len, slot, accept_skt;
char *cptr, buffer[128];
struct timeval tv;
memcpy(&tid_data, data, sizeof(tid_data));
accept_skt = tid_data.accept_skt;
slot = tid_data.slot;
buffer[sizeof(buffer)-1] = '\0';
while (1) {
tv.tv_sec = 120;
tv.tv_usec = 0;
setsockopt(accept_skt, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
len = recv(accept_skt, buffer, sizeof(buffer)-1, 0);
if (len == 0) {
if (!errno) continue;
// log
break;
}
if (len < 0) {
// log
break;
}
// validate user - omitted
// fill in cptr with OK or Denied -- omitted
len = send(accept_skt, cptr, strlen(cptr)+1, 0);
if (len <= 0) {
// log
break;
}
}
close(accept_skt);
pthread_exit(NULL);
return NULL; // Thread exits
}
“
在每次客户端连接和断开连接后,我执行以下操作并找到该线程的堆栈未释放:
[root @ xxx tmp] #pmap 12108 | grep 10240 | wc -l 0 [root @ xxx tmp] #pmap 12108 | grep 10240 | wc -l 1 [root @ xxx tmp] #pmap 12108 | grep 10240 | wc -l 2
为什么线程堆栈仍然与进程相关联?
我们将非常感谢您解决此问题的帮助。
答案 0 :(得分:0)