调用pthread_mutex_consistent_np 100%的CPU? [英] pthread_mutex_lock 100% cpu?
问题描述
code:
local void*
s_accept_connections(tmpsock)
void* tmpsock;
{
int32_t newfd;
int32_t tmp;
SOCKADDR_IN newsockaddr;
pthread_t id;
Connection* newconn;
const char *s;
char **splited;
int i;
StringVec *p;
StringVec* next;
Socket* sock;
tmp = sizeof(newsockaddr);
p = NULL;
next = NULL;
sock = (Socket *)tmpsock;
if (!sock)
return 0;
while (true){
newfd = accept(sock->fd,(SOCKADDR *)&newsockaddr,&tmp);
if (newfd <0){
if (check_error_async()){
pthread_mutex_lock(&g_socket_mutex);
#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
pthread_mutex_unlock(&g_socket_mutex);
continue;
}
}else{
newconn = (Connection *)MyMalloc(sizeof(*newconn));
newconn->fd = newfd;
newconn->addr = newsockaddr;
s = (const char *)inet_ntoa(newsockaddr.sin_addr);
p = split_string(s,".");
if (p != NULL){
splited = (char **)MyMalloc(sizeof(*splited) + 12);
i = 0;
for (; p != NULL; p = next){
if (p && p->next){
next = p->next;
}else{ break; }
splited[i] = p->value;
i++;
}
newconn->ip = swap_uint32_t((uint32_t)(atoi(splited[0])) + (atoi(splited[1]) << 8) + (atoi(splited[2]) << 16) + (atoi(splited[3]) << 24));
MyFree((char *)splited);
}else{
newconn->ip = 0;
}
newconn->closed = false;
newconn->state = 0;
newconn->state |= S_NEED_LOGIN;
pthread_mutex_init(&g_ping_mutex,NULL);
pthread_cond_init(&g_ping_cond,NULL);
pthread_create(&id,NULL,s_ping_thread,(void *)newconn);
a_conn(&sock->conn,newconn);
#ifndef NDEBUG
_("Accepting connection...\n");
#endif
if (sock->has_callback){
sock->func(newconn);
#ifndef NDEBUG
_("Accepted connection\n");
#endif
}
}
}
return 0;
}
void
start_accept(sock,join)
Socket* sock;
bool join;
{
pthread_t id;
pthread_attr_t attr;
if (!sock)
return;
if (!sock->conn){
sock->conn = (Connection *)MyMalloc(sizeof(*sock->conn));
if (!sock->conn)
return;
}
set_nonblocking(sock->fd);
set_nonblocking(sock->conn->fd);
pthread_attr_init(&attr);
pthread_mutex_init(&g_socket_mutex,NULL);
if (join){
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
}else{
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
}
pthread_create(&id,&attr,s_accept_connections,sock);
if (join){
pthread_join(id,NULL);
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&g_socket_mutex);
}
}
它只是提供了100%的CPU,任何想法?如果有更多的code需要,生病后
It simply gives 100% cpu, any ideas? if more code needed, ill post
推荐答案
是什么让你相信 pthread_mutex_lock()的
负责CPU使用率?
What makes you believe that pthread_mutex_lock()
is responsible for the CPU usage ?
使用调试器来找出发生了什么。
我的猜测是有一些问题与您的插座,使你的接受()
调用无阻塞。
Use a debugger to find out what is happening.
My guess is there is something wrong with your socket, making your accept()
call non-blocking.
检查返回值/消息(与 PERROR()
如果您运行的是Linux)。
Check the return value/message (with perror()
if you are running linux).
编辑:
您需要知道哪些code的片循环调试器可以帮你找到。
You need to know which piece of code is looping a debugger can help you to find this.
您有一个,而(真)
循环,很可能负责enless循环和100%的CPU使用率。它应该是好的,因为你有一个调用accept()(在这里: newfd =接受(sock-&GT; FD(SOCKADDR *)及newsockaddr,&安培; TMP);
)是应该停止线程/进程,直到下一个客户端连接。但是如果你的插座不正确初始化接受()
可以无需等待返回一个错误。
You have a while(true)
loop that is very likely to be responsible for the enless loop and the 100% CPU usage. It should be ok since you have a call to accept() (here :newfd = accept(sock->fd,(SOCKADDR *)&newsockaddr,&tmp);
) that is supposed to stop the thread/process until the next client connection. But if your socket is not correctly initialized accept()
may return an error without waiting.
这篇关于调用pthread_mutex_consistent_np 100%的CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!