1

I have a method which gets executed on another thread then the main thread. If it finishes, it calls a callback. But the main thread has to wait, otherwise it destroys the object where the callback wants to return.

Now, for simplicity, I have the following code:

int main()
{
    Something* s = new Something();
    s.DoStuff(); // Executed on another thread
    delete (s); // Has to wait for DoStuffCallback() to be executed
}

void Something::DoStuff()
{
    // Does stuff
    // If done, calls its callback
}
void Something::DoStuffCallback()
{
    // DoStuff has finished work
}

How can I wait until DoStuffCallback() got executed and then continue with the main thread?

Thanks a lot!

Edit:

This does not work for me, since I have not access to the right compiler. (I have as mentioned VS2010)

T.N.
  • 87
  • 10
  • 2
    Can you show how you are doing the threading? If you are using `std::thread` you just need to call `join` to wait for it to finish. – NathanOliver Nov 07 '18 at 14:02
  • You need to read a tutorial on threads, as even a pretty bad one should have told you what to do to wait for threads to finish. – Some programmer dude Nov 07 '18 at 14:02
  • @NathanOliver the thread gets created from within a .NET SDK. – T.N. Nov 07 '18 at 14:03
  • @NathanOliver Considering that the OP mentions VS2010, then probably not `std::thread`. – Some programmer dude Nov 07 '18 at 14:03
  • @T.N. Please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). How do you create your thread? Are you really programming in C++/CLI? What APIs are you using? – Some programmer dude Nov 07 '18 at 14:04
  • What does the comment, "Executed on another thread" mean? You're asking a question about a thread, but there's no code in your example that creates or even refers to a thread. – Solomon Slow Nov 07 '18 at 14:08
  • @Someprogrammerdude I am not creating the thread myself, rather then a SDK. I use a C++/CLI Wrapper to communicate between native C++ Code and .NET Code. The native C++ code starts DoStuff() which in .NET creates a new thread (I have no access to the actuall .NET Code) to execute it. Then, when DoStuff() is finished, its Callback DoStuffCallback() gets executed in the newly created thread. And I need to wait for DoStuffCallback() to get executed in my native C++ code. Perhaps that helps to understand my problem... – T.N. Nov 07 '18 at 14:09
  • @SolomonSlow as I said, I have no access to the .NET/C# Code that creates the thread. – T.N. Nov 07 '18 at 14:12
  • I would use a _[Semaphore](https://stackoverflow.com/a/4793662/801894)_. I would make the callback function signal the semaphore, and I would make the `main()` function await it. – Solomon Slow Nov 07 '18 at 14:17
  • But they are using std::mutex and condition_variable which I don't have access to. – T.N. Nov 07 '18 at 14:31
  • If your toolchain does not include the complete C++ standard library, then your options are (a) upgrade to a more modern toolchain, (b) install [boost](https://www.boost.org/), and use `boost::mutex`, etc., or (c) use platform-specific system calls. You haven't named the target platform. – Solomon Slow Nov 07 '18 at 15:08

1 Answers1

4

With Win32 Event

#include <windows.h>

int main()
{
    HANDLE handle = ::CreateEvent(NULL, TRUE, FALSE, NULL);

    Something s;
    s.DoStuff(handle); // Store the event handle and run tasks on another thread

    // Wait for the event on the main thread
    ::WaitForSingleObject(handle, INFINITE);
}

void Something::DoStuffCallback()
{
    // DoStuff has finished work
    ::SetEvent(m_handle);
}
Peter
  • 1,591
  • 6
  • 19
  • 1
    I dont know how many times I have to repeat myself. I have no access to the C++ 11 compiler, because I am using VS2010. – T.N. Nov 07 '18 at 14:44
  • I'm Sorry. I guess you are using windows. In this case you can create an event with `::CreateEvent` method and set this event with `::SetEvent` method when the callback is called. On the main thread you can wait for the event with `::WaitForSingleObject` – Peter Nov 07 '18 at 15:17
  • I've modified my answer from the C++11 solution to the windows-event stuff – Peter Nov 07 '18 at 15:35