Zellator Zellator - 14 days ago 11
C++ Question

C++/Threading : No instance of constructor "std::thread::thread" > matches the argument list

I've been having some problems with threading because I am very new to it.

I am getting a:


no instance of constructor "std::thread::thread"
matches the argument list

argument types are(void() )


Precisely at

std::thread t1(TestPlay);





void CMusicTCPDlg::OnBnClickedBtplaymusic()
{
std::thread t1(TestPlay);

t1.join();
}

void CMusicTCPDlg::TestPlay()
{
if (CFugue::GetMidiOutPortCount() <= 0)
{
std::cerr << "No MIDI Output Ports found!";
exit(-1);
}

std::cout << "Playing Notes..";
CFugue::PlayMusicStringWithOpts(_T("C D E F G A B"), MIDI_MAPPER, 20);
}





I've reffered to some threading pages and most had a simple example like mine.

Visual Studio advises me to use & before calling the function but it won't work with it.
Do I have to work with BackgroundWorker instead?

Really sorry if this is duplicate. Thank you!

Answer

TestPlay is a member function, this means its type of void (CMusicTCPDlg::)().

You will need to provide a bound version to allow the thread to call it: std::bind(&TestPlay, this). Be aware that you MUST make sure the thread does not exist for longer than the object itself or you will cause undefined behavior. (It will execute a function on a non-existant object)