struct taskinfo
{
        long int id;
        bool cancel;
        std::function<void()> func;
        std::chrono::system_clock::time_point time;
        std::chrono::system_clock::duration interval;
        taskinfo(){ }
        bool operator<(const taskinfo& task) const {
            return time > task.time;
        }
        public:
        taskinfo(long int id, std::function<void()>&& f, const std::chrono::system_clock::time_point& t)
                : id(id), func(f),
                time(t)
        {
                cancel = false;
        }
}
....
std::priority_queue<taskinfo, std::vector<taskinfo>> tasks;
void at(taskinfo** task){
        std::function<void()> threadFunc = [task]() { std::thread((*task)->func).detach(); };
        (*task)->func = threadFunc;
        tasks.push(**task);
}
In main()..
std::vector<taskinfo*> requests;
for(int i=1; i <=5; i++ )
{
        taskinfo* t = new taskinfo(i, [i]{ timeoutFunc(i); }, std::chrono::system_clock::now() + std::chrono::milliseconds(timeout));
        tT.at(&t);
        requests.push_back(t);
        std::cout << "Request " << i << " Registered.... Time:" << std::chrono::system_clock::now().time_since_epoch().count() << std::endl;
}
I think I am missing something here when I pop the function out of the queue to execute, the function may be empty, Nothing is getting executed.
If i copy taskinfo to locally
void at(taskinfo** task){
            taskinfo t = **task;
            //Replace everything else with t function works fine But 
            //I need to modify the same reference
}
How can I work with pointer reference herein lambda?
I have added the complete code of what i am trying to do here.
Complete Code:
#include <functional>
#include <chrono>
#include <future>
#include <queue>
#include <thread>
#include <memory>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <ctime>
#include <sys/time.h>
#include <unistd.h>
#include <limits.h>
#define TIMER_NO_TASK_SLEEP_TIME        100
struct taskinfo
{
        long int id;
        bool cancel;
        std::function<void()> func;
        std::chrono::system_clock::time_point time;
        std::chrono::system_clock::duration interval;
        taskinfo(){ }
        bool operator<(const taskinfo& task) const {
                return time > task.time;
        }
        public:
        taskinfo(long int id, std::function<void()>&& f, const std::chrono::system_clock::time_point& t)
                : id(id), func(f),
                time(t)
        {
                cancel = false;
        }
};
class TimerTask
{
        private:
                std::priority_queue<taskinfo, std::vector<taskinfo>> tasks;
                std::unique_ptr<std::thread> thread;
                bool keepRunning;
        public:
                TimerTask()
                        :keepRunning(true),
                        thread(new std::thread([this]() {
                                                while(keepRunning)
                                                {
                                                auto now = std::chrono::system_clock::now();
                                                while(!tasks.empty() && tasks.top().time <= now) {
                                                if(!tasks.top().cancel)
                                                {
                                                tasks.top().func();
                                                }
                                                tasks.pop();
                                                }
                                                if(tasks.empty()) {
                                                std::this_thread::sleep_for(std::chrono::milliseconds(TIMER_NO_TASK_SLEEP_TIME));
                                                } else {
                                                std::this_thread::sleep_for(tasks.top().time - std::chrono::system_clock::now());
                                                }
                                                }
                                                })){ }
                ~TimerTask()
                {
                        keepRunning = false;
                        thread->join();
                }
                //Execute a task when the timer times out
                void at(taskinfo** task){
                        std::function<void()> threadFunc = [task]() { std::thread((*task)->func).detach(); };
                        (*task)->func = threadFunc;
                        tasks.push(**task);
                }
                //Cancel the particular task with a flag
                void cancel(taskinfo** task){
                        (* task)->cancel = true;
                }
};
//The return type of the task must be void
void timeoutFunc(int id)
{
        std::cout << "Request " << id << " Timeout.... Executed Timeout Function Time:" << std::chrono::system_clock::now().time_since_epoch().count() << std::endl;
}
int main(int argc, char* argv[])
{
        if(argc != 2)
        {
                std::cout << "\n Usage <Process> <Timeout>" << std::endl;
                return 0;
        }
        int timeout = atoi(argv[1]);
        TimerTask tT;
        std::vector<taskinfo*> requests;
        requests.reserve(1000);
        for(int i=1; i <=5; i++ )
        {
                taskinfo* t = new taskinfo(i, [i]{ timeoutFunc(i); }, std::chrono::system_clock::now() + std::chrono::milliseconds(timeout));
                tT.at(&t);
                requests.push_back(t);
                std::cout << "Request " << i << " Registered.... Time:" << std::chrono::system_clock::now().time_since_epoch().count() << std::endl;
        }
        while(1) sleep(60);
        return 0;
}
 
    