Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Pointer, point to pointer, again

Hi everyone,

Sorry to beat this subject to death, I want to make sure I am right in doing this. Here's my setup:

struct ThreadData {
    ThreadData (Connection* pConnection)
    {
         m_pConnection = pConnection;
    }
    Connection* m_pConnection;
    // some other junk here too.
};

int main()
{
     while (connections)
     {
          Connection* pConnection  =  0;
          ThirdPartyLibConnectFunction(pConnection); // this calls malloc inside on pConnection.

           // Now I want a thread to handle the connection.
           ThreadData* pThreadData = new ThreadData(pConnection);
           RunThread(pThreadData);
      }
}

void RunThread(void* pParam)
{
     auto_ptr<ThreadData*> pData((ThreadData*)pParam);

     // Do stuff with the connection object now.
     pData->m_pConnection->Whatever();
}
         
My question is, is my use of pointers here doing what I want? (tought to read my mind I know). Ok I want the data structure allocated in the main thread to be used only by the handler thread, and to be cleaned up in the thread handler. I think this is doing it right?:

     m_pConnection = pConnection;

So the actual memory is being allocated in the main thread for it, but the main thread cannot touch it anymore after the current iteration of the while loop goes through. Then the handler thread can play with it however it wants to and can deallocate it properly.

Is this correct?

Thanks for your help as always.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Alright thanks just needed someone to proof read it,

Thanks
You're most welcome ;o)