我试图在我的Qt应用程序中集成boost :: thread的功能,但编译器会产生错误。我是不是对boost :: thread的新东西,事实上我在非qt应用程序中使用了很多次,但出于某种原因我遇到了这个问题。这是确切的代码:
头文件:
#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
源文件:
#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文件:
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
我运行项目并且:
/usr/include/boost/bind/bind.hpp:259: error: too many arguments to function
unwrapper<F>::unwrap(f, 0)(a[base_type::a1_]);
~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
之前我在许多不同的非Qt项目中使用过这个很棒的库,我从来没有遇到任何问题。有没有解决这个问题?
我的所有API都基于boost :: thread。
我可以使用Qt线程,但我不想。
无论如何,现在,我想让boost线程起作用。
答案 0 :(得分:3)
my_lengthly_method
是静态方法,因此this
是多余的,只需调用
boost::thread(&my_lengthly_method);
在上面的行中你创建了一个临时的线程对象,在执行这一行后,线程临时对象被销毁了,在这个地方你可能会遇到问题,因为在调用std::thread
的析构函数而不调用{join
的情况下,在C ++标准库中调用它上面的{}}} - 您的应用已关闭。在BOOST中,它取决于您的库的构建方式,如果使用define std::terminate
,那么您的代码将起作用。但为了安全起见,您应该为对象命名并调用BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
方法。
deatch