Solved

Pointer, point to pointer, again

Posted on 2007-11-25
3
139 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:DJ_AM_Juicebox
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20346865
>>Is this correct?

In short: Yes.

Hard to add anything, you wrote it all.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20346893
Alright thanks just needed someone to proof read it,

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 20346944
You're most welcome ;o)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now