Reputation: 327
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.
Upvotes: 0
Views: 558
Reputation: 20959
my_lengthly_method
is static method so this
is redundant, just call
boost::thread(&my_lengthly_method);
In above line you create a temporary thread object and after executing this line the thread temporary object is destroyed, in this place you may have problem because in C++ standard library when destructor of std::thread
is called without calling join
on it std::terminate
is called - your app is closed. In BOOST it depends on how your library was built, if with define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
then your code will work. But for safe you should name your object and call deatch
method.
boost::thread myThread(&my_lengthly_method);
myThread.detach();
}
Upvotes: 3