开发者

pthread_mutex_lock 100% cpu?

开发者 https://www.devze.com 2023-02-18 15:14 出处:网络
code: local void* s_accept_connections(tmpsock) void* tmpsock; { int32_t newfd; int32_t tmp; SOCKADDR_IN newsockaddr;

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){
    newf开发者_如何学运维d = 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);
      }
    }

It simply gives 100% cpu, any ideas? if more code needed, ill post


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.

Check the return value/message (with perror() if you are running linux).

EDIT :

You need to know which piece of code is looping a debugger can help you to find this.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消