0
votes

I would like to know is this is possible. Create a shared_ptr or unique_ptr of TestClass type.

Then call _beginthreadex and pass it a static method of the class as the function to execute and the shared_ptr or unique_ptr created before as data. Like this:

shared_ptr<TestClass> p = make_shared<TestClass>(count, "test");
HANDLE hth1 = (HANDLE)_beginthreadex(NULL, 0, p->ThreadStart,p, 0, NULL);

I normally use this approach without smart pointers, I usually create a normal pointer of TestClass and pass a static method of the TestClass and the pointer itself as data, then inside the static method I cast it to (TestClass *) and run the member methods of the class etc, do the work and when the thread is done I delete the pointer. Something like this:

    TestClass * p = new TestClass(count, "test");
    HANDLE hth1 = (HANDLE)_beginthreadex(NULL, 0, p->ThreadStart,p, 0, NULL);

What I want to achieve is to make the smart pointer to delete the object automatically when the thread ends because the smart pointer would be out of scope.

When I do it the way I am describing it above the compiler shows this error:

"no suitable conversion function from "std::shared_ptr" to "void *" exists"

2
Did you consider using standard thread library? Then it would be perfectly possibleRafal Mielniczuk
That is my next move if I cant do it like I tried firstuser3196371
This seems like a particularly silly use of shared_ptr. Oh, let's use this shared pointer. But it's shared and we don't want it to be shared. That's okay, we'll hack around to make it act like it's not shared. But then ... why do it at all? This would make perfect sense if you already had a shared_ptr or needed one for some reason (maybe the thread needs to extend the object's lifetime). But to create one just to undo its sole purpose ... crazy!David Schwartz
David, I don't think that you understood what I asked, but it is ok, txs anywaysuser3196371

2 Answers

2
votes

_beginthreadex looks like c function, and to achieve what you want, you need to invoke copy constructor (or move in case of unique_ptr) to transfer ownership, which is not possible in c.

You may consider using std::thread class which plays nice with the rest of std suite.

Assuming you want to run:

void ThreadStart(std::shared_ptr<TestClass> p);

You can fire that by

void ThreadStart(std::shared_ptr<TestClass> p)
{
    cout << p->count << " " << p->name << endl;
}

int main()
{
    shared_ptr<TestClass> p = make_shared<TestClass>(33, "test");
    std::thread thr(ThreadStart, p);
    thr.join();
}

In case of std::unique_ptr you need to std::move it to the thread function, so it will be actually deleted at the thread's end:

void ThreadStart(std::unique_ptr<TestClass> p);
...
unique_ptr<TestClass> p = make_unique<TestClass>(33, "test");
std::thread thr(ThreadStart, std::move(p));
0
votes

Use shared_ptr::get or unique_ptr::get to get access to a pointer to the managed object.

shared_ptr<TestClass> p = make_shared<TestClass>(count, "test");
HANDLE hth1 = (HANDLE)_beginthreadex(NULL, 0, p->ThreadStart, p.get(), 0, NULL);