James Campbell
James Campbell

Reputation: 3591

C++11 Timer Function?

IS there some kind of non blocking timer I can use in my main thread that would invoke a function that would read a message queue to see if any worker threads had given me useful information to update the GUI With ?

Or would I have to resort to old fashioned polling / update when needed.

Is there some way of scheduling updates ? I know you can't have cross thread call backs i.e my worker thread runs the call back on the main thread and I'm not sure you can do this even with a continuation class.

I was wondering however if I could use a abstraction layer to pull it off, for instance in iOS I can easily run things on the main GUI Thread using GCD and Windows 8 has a way of having a function running once a future has completed on the thread it was invoked from. And I'm guessing for Android because you are using JNI to interface to the VM, none of the C++ threads are the GUI Threads so it doesn't actually matter.

So I could write a piece of code that abstracts this for each platform ?

Upvotes: 3

Views: 1040

Answers (1)

Gabriel
Gabriel

Reputation: 3594

the comment thomas posted it good so that's clearly the best link Apart from it I have made an example that can help.

#include <iostream>
#include <stdio.h>
#include <thread>
using namespace std;
int main()
{
    std::thread([]() {std::this_thread::sleep_for(std::chrono::milliseconds(1000));
                      cout<<"end of the thread\n";
                    }).detach();

    for (int i=0;i<20;++i)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        cout<<i<<std::endl;
    }
    return 0;
}

And my output is: 0 1 2 3 4 5 6 7 8 end of the thread 9 10 11 12 13 14 15 16 17 18 19

Hope that helps :-)

Upvotes: 3

Related Questions