7

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!

Cattani
  • 124
  • 1
  • 9

1 Answers1

11

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)

Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • 1
    As an alternative the function could be declared as static, if no access to any of it's members is required (which looks like this in the given code). – Matthias247 Nov 23 '16 at 17:47
  • I see, this definitely clears things up! Thank you so much,guys! – Cattani Nov 23 '16 at 17:53
  • 1
    [Start thread with member function](https://stackoverflow.com/questions/10673585/start-thread-with-member-function) - std::thread can take both a member function and the instance pointer – jozxyqk Nov 16 '19 at 01:17