4

I am writing an MFC program in Visual Studio 2013 and i keep getting the two following errors

Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'

and

Error C2672 'std::invoke': no matching overloaded function found

The error is related to file xthread line 238

I am fairly new at c++/MFC and I am trying to write a function that will run in the background to the system time.

This is the code i am using:

void task1(ExperimentTab& dlg)
{
    while (true)
    {
        CString showtime = CTime::GetCurrentTime().Format("%H:%M:%S");
        int x = dlg.m_showTime.GetWindowTextLengthA();
        dlg.m_showTime.SetWindowTextA(_T(""));
        dlg.m_showTime.ReplaceSel(showtime, 0);
    }
}

void mainThread()
{
    std::thread t1(task1);
    t1.join();
}

This is then being called on a button press to start the time, but the same button is used to also stop the time.

Liam P
  • 217
  • 5
  • 13
  • Very little to gain from multithreading here. Use a timer (see [CWnd::SetTimer](https://msdn.microsoft.com/en-us/library/49313fdf.aspx)) instead, and keep everything on a single thread. Added bonus: You won't burn an entire CPU core, just to update the current time. And you can actually close your process, without taking to the task manager. – IInspectable May 19 '16 at 22:31

1 Answers1

5

Function task1 takes single argument (used as thread body), yet you pass none in t1 constructor. Compiler fails to create std::invoke calling task1 function without method argument.

To fix it call constructor like that: std::thread t1(task1, std::ref(dlg));, where dlg is ExperimentTab. std::ref ensures that dlg will be passed to thread via reference.

BTW: updating MFC component from other thread might lead to some data races. Also - while(true) thread will consume 100% of CPU by multiple updates per second of timer with second resolution.

Hcorg
  • 11,598
  • 3
  • 31
  • 36
  • Thank you for the quick response! I tried your solution but it just gives me the error `Error C2280 'ExperimentTab::ExperimentTab(const ExperimentTab &)': attempting to reference a deleted function` – Liam P May 19 '16 at 17:07
  • Maybe I'm not doing this right... but now I'm getting an error saying unresolved external symbol LNK2019 – Liam P May 19 '16 at 17:13
  • so code compiles, now you have missing link time dependencies (not all files included in project? missing library?) – Hcorg May 19 '16 at 17:18
  • @LiamP LNK2019 should list which symbol could not be found. If it's not one of your two functions above, you'll need a new question. Sadly that question would almost instantly be closed as a duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user4581301 May 19 '16 at 17:26
  • Okay i sorted that out. I missed commenting out another function call. Sorry just one more question, I'm still really learning this, I can't figure out what to put when calling `mainThread();` I get the error `too few arguments in function call` – Liam P May 19 '16 at 17:27
  • @LiamP `mainThread();` call looks good for code you've pasted, maybe error points to different line? – Hcorg May 19 '16 at 17:41
  • void mainThread(ExperimentTab& dlg) { std::thread t1(task1, std::ref(dlg)); t1.join(); } – Liam P May 19 '16 at 17:43
  • When i do that it says `type name is not allowed` – Liam P May 19 '16 at 17:48