-2

The title says it all. Basically want to be able to activate a function at a certain time, but I want to do this without interrupting the user. Previously, I played with this idea using sleep_until:

cout << "set time" << endl;
cin >> time;
sleep_until (system_clock::from_time_t (timegm(&time)));
someFunction();

The problem is, the user can't do anything once sleep_until starts. What I want to happen is for the user to be able to continue doing things with the program uninterrupted and for the function to activate at the specified time. How would I go about doing this?

EDIT: To clarify, I want the user to do different things in my program until the time has come for the function to activate.

EDIT2: When trying to get the thread going:

In Schedule class:

void Schedule::timeAlert(tm time) {
    sleep_until (system_clock::from_time_t (timegm(&time)));
    cout << "It is " << std::asctime(&time);
}

In main:

Schedule mySchedule    
std::thread t(mySchedule.timeAlert, time);
t.join();

EDIT3: I have tried this method too:

std::thread t(&Schedule::timeAlert, this, time1);
t.join();

But I get this problem...

error: invalid use of ‘this’ in non-member function
  std::thread t(&Schedule::timeAlert, this, time1);

Despite timeAlert being in public under my .h file and being labeled with Schedule:: in my cpp file.

Monzaku
  • 57
  • 5
  • 3
    You can launch the function in a separate thread. – Govind Parmar Feb 05 '19 at 20:00
  • you can use a daemon process or task scheduler for Windows – bogdan tudose Feb 05 '19 at 20:04
  • Clarified in the main post. I want them to be able to do different things in my program. That said how would you launch a new thread for the function? – Monzaku Feb 05 '19 at 20:04
  • 1
    [`std::async`](https://en.cppreference.com/w/cpp/thread/async) is probably the easiest way to perform an asynchronous task. Though beware that, as soon as you start playing around with multithreading you expose yourself to race conditions and other new complexities that you don't have to worry about in single threaded contextes. Be sure to understand how thread synchronization works. – François Andrieux Feb 05 '19 at 20:05
  • Take advantage of the event loop and a timer + callback function. – Jesper Juhl Feb 05 '19 at 20:11
  • If you wish to stay within Standard C++ [start here](https://en.cppreference.com/w/cpp/thread/thread) For tools to help defend against some of the nasties François mentioned, see [the support library](https://en.cppreference.com/w/cpp/thread). – user4581301 Feb 05 '19 at 20:15

1 Answers1

0

Just use a dummy function and a thread:

#include <thread>
#include <chrono>
#include <iostream>

using namespace std;

void invokeSomeFunctionAfter(int time) {
    //since we wait in another thread the main function will not be blocked
    this_thread::sleep_for(chrono::milliseconds(time));
    cout << "I was called after " << time  << "ms" << endl;
    someFunction();
}

int main() {
    cout << "set time" << endl;
    int time;
    cin >> time;
    //start a new thread with the given time which will wait
    thread t(invokeSomeFunctionAfter, time);
    //do more stuff

    //wait for the thread to terminate
    t.join();
    return 0;
}

So the //do more stuff part will be called instantly after starting the thread. If you need to sync up with the threads later in your code you can use t.join() so your current thread will block until the thread has terminated.

EDIT Regarding your update:

When invoking methods inside classes please checkout this answer. You need to change you code like this:

Schedule mySchedule    
std::thread t(&Schedule::timeAlert, &mySchedule, time);
t.join();
Crigges
  • 1,083
  • 2
  • 15
  • 28