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

标签 c++ multithreading

我在线程方面遇到了一些问题,因为我对它很陌生。

我得到一个:

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

argument types are(void() )

恰好在

 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);
    }

我引用了一些线程页面,大多数都有一个像我这样的简单示例。

Visual Studio 建议我在调用该函数之前使用 &,但它无法使用它。 我必须改为使用 BackgroundWorker 吗?

如果这是重复的,真的很抱歉。谢谢!

最佳答案

TestPlay 是一个成员函数,这意味着它的类型是void (CMusicTCPDlg::)()

您需要提供绑定(bind)版本以允许线程调用它:std::bind(&TestPlay, this)。请注意,您必须确保线程存在的时间不超过对象本身,否则将导致未定义的行为。 (它将在一个不存在的对象上执行一个函数)

关于C++/线程 : No instance of constructor "std::thread::thread" > matches the argument list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40770913/

相关文章:

Android:Java线程如何对应原生线程?

c++ - SSE 移位指令在后续指令中导致奇怪的输出 (-1.#IND00)?

c - 使用多线程将大数相乘

python - Tcl_AsyncDelete 错误多线程 Python

c++ - 链接的外部 DLL 中 std::vector 的 Mex 动态内存管理问题;段错误

multithreading - 使用AHK_H v2,如何让多个线程共享并更新同一个变量?

python - 对带有 while 循环的函数使用多线程?

c++ - 如何修复错误 "could not convert <brace enclose initilizer list> to Node"

c++ - Arduino 十六进制转十进制

c++ - 使用 Eclipse 时如何向我的 C++ 编译可执行文件添加图标?