c++ - c++ boost::thread() in Qt application - error: too many arguments to function
问题描述
I am trying to integrate the functionality of boost::thread in my Qt applications but the compiler produces an error. I am not new to boost::thread, as a matter of fact I have used it many, many times in non-qt applications but for some reason I am having issues with this one. Here is the exact code:
header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <boost/thread.hpp>
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
static void my_lengthly_method();
};
#endif // MAINWINDOW_H
source file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
boost::thread(&my_lengthly_method, this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::my_lengthly_method()
{
}
.pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
INCLUDEPATH += $$PWD
TARGET = untitled
TEMPLATE = app
LIB_GLOBAL = /usr/lib/x86_64-linux-gnu
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
LIBS += \
-L$$LIB_GLOBAL -lboost_system \
-L$$LIB_GLOBAL -lboost_filesystem \
-L$$LIB_GLOBAL -lboost_thread \
-L$$LIB_GLOBAL -lboost_regex
I run the project and:
/usr/include/boost/bind/bind.hpp:259: error: too many arguments to function
unwrapper<F>::unwrap(f, 0)(a[base_type::a1_]);
~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
When you click on the error, it opens up that file and here is what you get:
I have used this awesome library in many different non Qt projects before and I have never had any issues. Is there any work around for this?
All of my APIs are based around boost::thread.
I can use Qt threads but I don't want to.
Anyway, right now, I want to get the boost thread thing to work.
解决方案
my_lengthly_method
是静态方法所以this
是多余的,只需调用
boost::thread(&my_lengthly_method);
在上面的行中,您创建了一个临时线程对象,并且在执行此行之后,线程临时对象被销毁,在这个地方您可能会遇到问题,因为在 C++ 标准库中,当调用析构函数std::thread
而不调用join
它时std::terminate
- 您的应用程序已关闭。在 BOOST 中,这取决于您的库是如何构建的,如果使用定义,BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
那么您的代码将起作用。但为了安全起见,您应该命名您的对象并调用deatch
方法。
boost::thread myThread(&my_lengthly_method);
myThread.detach();
}
推荐阅读
- python - 如何使用熊猫在 dataFrame 中创建句点?
- modx - 如何在 MODX 集合子网格中配置下拉列表?
- types - 在 Julia 中将 DataType 转换为 UnionAll
- javascript - 为什么在我的 JS 弹出窗口中我的 html 表单不起作用?
- javascript - 发送和处理表单数据
- swift - 无法从外部 userCompletionHandler 访问值
- google-compute-engine - 如何让应用内菜单在 Google Compute Engine 的 GUI 中可靠工作?
- r - 为什么即使数据发生了变化,情节也不会更新
- php - 使用 RegEx 选择千位分隔符
- javascript - Bootstrap:如何通过单击单个选项卡来显示 2 个选项卡内容?