Click here to Skip to main content
14,733,301 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more: , +
I have created 3 threads each thread has one socket each. Inside each thread, socket is made "Event Driven" and whenever data becomes available for reading, an event is generated.

The code works fine but it takes CPU Usage upto 100% which is surely undesirable. I think I have made some mistake. Below is my code. Please help me in figuring out what mistake have I made that results in 100% CPU Usage.

Code:

DWORD WINAPI ThreadProc(LPVOID param)
{
   int threadNumber= (int)param;
   int PORT = 8888+threadNumber; //so that each thread bind()s its socket to a different Port number.
   WSADATA wsa; 

   //Initialise winsock//
   if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
      {

        //"WinSock Initialization FAILED",

      }

   //Create a socket//

  SOCKET newSocketIdentifier;
  SOCKADDR_IN newSocket;

  if((newSocketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
      {                 

        //Socket Creation Failed

      }
   //Socket Created//

   //Prepare the sockaddr_in structure//
  newSocket.sin_family = AF_INET;
  newSocket.sin_addr.s_addr = INADDR_ANY;
  newSocket.sin_port = htons(PORT);

   //Bind//
   if( bind(newSocketIdentifier ,(struct sockaddr *)&newSocket, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
      { 
       //Bind Failed
      }

   //Bind Done//
char data[256];
int bytes, waitRet;

WSAEVENT hEvent = WSACreateEvent();
WSANETWORKEVENTS events;
WSAEventSelect(newSocketIdentifier, hEvent, FD_READ | FD_WRITE);

   while(1)
      {
         waitRet = WSAWaitForMultipleEvents(2, &hEvent, FALSE, WSA_INFINITE, FALSE);
         if(WSAEnumNetworkEvents(newSocketIdentifier,hEvent,&events) == SOCKET_ERROR)
            //Error
         else
            {
               if(events.lNetworkEvents & FD_READ)
                  {
                      //call recvfrom()
                  }
            }
      }

    WSACloseEvent(hEvent);
   return 0;
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{

   HANDLE threadHandle[3];
   int max_number=3;
   //Start the threads
   for (int i = 0; i < max_number; i++) 
   {

    threadHandle[i]= CreateThread( NULL,
                                       0,
                                       ThreadProc,
                                       (LPVOID) i,
                                       0,
                                       NULL
                                 );
   }

return 0;
}
Posted
Updated 7-May-13 21:51pm
v5

i guess your threads are blocking the network api.

why arent you making some outout or use your debugger...
   
Did you report errors and terminate the thread when a function call fails?

There is at least one wrong call that should result in an error return (best case) or undefined behaviour (worst case):
WSAWaitForMultipleEvents(2, &hEvent, FALSE, WSA_INFINITE, FALSE);

You are passing 2 for the number of events and the address of a single hEvent.
So it must be one of these:
// Single event
WSAWaitForMultipleEvents(1, &hEvent, FALSE, WSA_INFINITE, FALSE);

// Two events
WSAEVENT hEvents[2];
hEvents[0] = hEvent;
hEvents[1] = hOtherEvent;
WSAWaitForMultipleEvents(2, hEvents, FALSE, WSA_INFINITE, FALSE);
   
Comments
ayesha hassan 8-May-13 2:05am
   
+1, Problem Solved :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900