harry
harry

Reputation: 105

Multi threading for reading and writing to SERVER in c++

I have a requirement where I have to write on socket and then read response continuously from socket and process the data. I have created 2 classes A and B. A has write() and read() api and B has processdata() api. I have created callback from A::read() to B::processdata(). But i am facing issue with multi threading as as i am new to it. Thread to read() api has to be always running, but while processing of data in B::processdata(), server is sending some more data on socket read() which i am missing. Please suggest some design for this issue so that i can keep storing data while my processdata function may finish its work and can comeback again to read data (with small example)? I was thinking of maintaining 3 threads, 1 for each write , read and processdata. But I am not sure how to comeback to read thread after finishing processdata thread.

Sorry for the long post but I will appreciate if someone can help me with this.

Below is my very high level code design which I am having in my project.

 //A.cpp
        class A {
        public:
          void  write();
           void read(b* obj);
        }
        void A::write()
        { 
           //code to write to socket  

          }
        void A::read(b* obj)
        { 
           //code to read from socket  
          // if data received call below function 
          obj->processdata(buffer)
          }

    //B.cpp    
        class B {
        public:
            processdata(buffer)
        }

        void B::processdata(buffer)
        { 
           //code to processdaata from socket  
          }
//Main.cpp
        int main()
        {
          A* objA = new A;
          B* objB = new B;
          objA->write()
          while(1)
           {
             objA->read(objB)
            }
        }

Upvotes: 1

Views: 1850

Answers (1)

Akhil V Suku
Akhil V Suku

Reputation: 912

A.cpp

extern  CRITICAL_SECTION    g_cCritSec;
        class A {
        public:
          static void  write();
          static void read(void* obj);
        }
        void A::write()
        { 

           EnterCriticalSection(&g_cCritSec);
           //code to write to socket 
           LeaveCriticalSection(&g_cCritSec); 

        }
        void A::read(void* obj)
        { 
           EnterCriticalSection(&g_cCritSec);
           while(1)
           {
              //code to read from socket  
              // if data received call below function 
              // send separate copy of buffer to avoid overrite
              uintptr_t Thread2 = _beginthread( B::processdata, 0, (void *) buffer); 

              //obj->processdata(buffer)
           }
           LeaveCriticalSection(&g_cCritSec);
        }

B.cpp

  extern    CRITICAL_SECTION    g_cCritSec;
        class B {
        public:
            static processdata( void *buffer)
        }

        void B::processdata(void *buffer)
        {  
           Buffer *bufferReceive = (Buffer*)buffer;
           //code to processdaata from socket   
        }

Main.cpp

CRITICAL_SECTION    g_cCritSec;
        int main()
        {

          InitializeCriticalSection(&g_cCritSec); 
          B* objB = new B;
          objA->write();
          uintptr_t Thread1 = _beginthread( A::read, 0, (void *) objB); 

          DeleteCriticalSection(&g_cCritSec); 
        }

will this answer helps you ? A minimal multithreaded application that will start Thread1 as the static memeber funcation A::Read as the routine(we need to define the memeber function as static to use as thread call back). From Read Function it will start another thread with copy of buffer passing to it. Here both the threads will executes simultaneously

Upvotes: 1

Related Questions