Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4082692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:13:08+00:00 2026-05-20T18:13:08+00:00

code: local void* s_accept_connections(tmpsock) void* tmpsock; { int32_t newfd; int32_t tmp; SOCKADDR_IN newsockaddr; pthread_t

  • 0

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);
      }
    }

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:13:09+00:00Added an answer on May 20, 2026 at 6:13 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code runs on my local RoR/Windows 7 (64-bit): sql = ActiveRecord::Base.connection() last_pk =
Here's the code: // allocation void allocateSymbolStorage(char **pepperShakerList, char **pepperList) { // allocate storage
How to split mobile number into country code, area code and local number? e.g
I have the following setup: Code on my local machine (OS X) shared as
I had no problem at all running the following code on a local server,
This code used to return my local ip address as 192.xxx.x.xxx but now it
The following code fails to compile stating A local variable named 'st' cannot be
I have a codeigniter code that is working on local Apache installation. But it
There Currently is a local debate as to which code is more readability We
ms-access is connecting to a local mysql database the following code returns an error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.