【问题标题】:Posix thread in QtQt中的Posix线程
【发布时间】:2017-11-16 04:15:55
【问题描述】:

我收到一个错误:

非静态成员函数的使用无效

基本上,每当我收到消息时,我都需要更新线程中的 UI:

if (pthread_create(&thread, NULL, MainWindow::thread_message, &sock))
{
    perror("error in creating the thread");
    // return EXIT_FAILURE;
}

void *MainWindow::thread_message(void *arg)
{
    // UI update, whenever I receive a message from the server.
}

【问题讨论】:

标签: c++ qt pthreads posix non-static


【解决方案1】:

您需要声明 MainWindow 类的 thread_message 静态或具有非类/结构绑定函数,因为 pthread_create 需要经典的 c 样式函数指针,并且这些指针和指向类/结构方法的指针之间存在差异C++。

为了克服这个问题,C++11 引入了 std::thread ,它接受这两种类型的函数指针并使您不必在代码中使用依赖于平台的线程函数。

【讨论】:

    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多