samedi 27 février 2016

passing template args to std::thread

Im having a little problem passing the args to my std::thread that calls the template function.

template <typename F, typename... A>
void start(int interval, F func, A&&... args)
{
    if (_running.load(std::memory_order_acquire)) {
        stop();
    };
    _running.store(true, std::memory_order_release);
    _thread = std::thread([this, interval, func, args]()
    {
        while (_running.load(std::memory_order_acquire))
        {
            func(std::forward<A>(args)...);

            for (int i = 0; (i <= (interval / 200)) && _running.load(std::memory_order_acquire); i++)
                std::this_thread::sleep_for(std::chrono::milliseconds(200));
        }
    });
}

edit: Added a little more detail I should be able to pass any function to "start" with any ammount or args

void ThreadProc(HWND hwnd)
{
    Beep(1000, 100);
}

start(2000, ThreadProc, hwnd);

error:

error C3520: 'args': parameter pack must be expanded in this context
    note: see reference to function template instantiation 'void CallBack::start<void(__cdecl *)(HWND),HWND&>(int,F,HWND &)' being compiled
    1>          with
    1>          [
    1>              F=void (__cdecl *)(HWND)
    1>          ]

Aucun commentaire:

Enregistrer un commentaire