我将应用程序移植到Windows上,并发现当跨越DLL边界中断boost :: thread时,线程不会被中断。
boost库是静态链接的,使用调试运行时和链接到共享运行时库的多线程支持构建。 / MDD
该项目使用msvc12(2013)编译(与编译boost相同的编译器)。
我做了一个简短的示例项目,演示了这个问题:
TEST.CPP
#include <iostream>
#include "boost/thread.hpp"
#include "test_library.h"
#include <iostream>
int main()
{
qDebug() << "Hello";
Test_library test;
qDebug() << "Starting thread";
boost::thread thread = boost::thread(&Test_library::Loop, &test);
boost::this_thread::sleep_for(boost::chrono::seconds(3));
qDebug() << "interrupt thread";
thread.interrupt();
qDebug() << "Joining thread";
thread.join();
qDebug() << "Closing Program";
return 0;
}
test_library.cpp
#include "test_library.h"
#include "boost/thread.hpp"
void Test_library::Loop()
{
qDebug() << "Starting loop";
while(true)
{
qDebug() << "Looping";
boost::this_thread::interruption_point();
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
}
qDebug() << "Looping finished";
}
test_library.h
#pragma once
#include "test_library_global.h"
#include <QtCore>
class TEST_LIBRARYSHARED_EXPORT Test_library
{
public:
void Loop();
};
TestProject.pro(QMake文件)
DESTDIR = ../TestProjectDeploy/
SOURCES += \
test.cpp
LIBS += -L$$DESTDIR -ltest_library
include(../BoostIncludes.pri)
DESTDIR = ../TestProjectDeploy/
QT -= gui
TARGET = test_library
TEMPLATE = lib
DEFINES += TEST_LIBRARY_LIBRARY
SOURCES += test_library.cpp
HEADERS += test_library.h
TestLibrary.pro
include(../BoostIncludes.pri)
DESTDIR = ../TestProjectDeploy/
QT -= gui
TARGET = test_library
TEMPLATE = lib
DEFINES += TEST_LIBRARY_LIBRARY
SOURCES += test_library.cpp
HEADERS += test_library.h\
test_library_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
BoostIncludes.pri
win32:INCLUDEPATH += "C:/Libraries/boost_1_60_0/"
LIBS = "-LC:/Libraries/boost_1_60_0/stage/lib/" \
"C:/Libraries/boost_1_60_0/stage/lib/libboost_thread-vc120-mt-gd-1_60.lib" \
"C:/Libraries/boost_1_60_0/stage/lib/libboost_system-vc120-mt-gd-1_60.lib" \
"C:/Libraries/boost_1_60_0/stage/lib/libboost_chrono-vc120-mt-gd-1_60.lib" \
"C:/Libraries/boost_1_60_0/stage/lib/libboost_timer-vc120-mt-gd-1_60.lib" \
节目输出:
Hello
Starting thread
Starting loop
Looping
Looping
Looping
Looping
Looping
Looping
interrupt thread
Looping
Joining thread
Looping
Looping
Looping
Looping
Looping
Looping
答案 0 :(得分:1)
此问题是由于未动态链接到boost线程dll。 所有项目中都需要预处理器宏
DEFINES += BOOST_THREAD_USE_DLL
################### WINDOWS ###################
WINDOWS_BOOST_DEBUG_DLL = "-LC:/Libraries/boost_1_60_0/stage/lib/" \
-l"boost_system-vc120-mt-gd-1_60"\
-l"boost_chrono-vc120-mt-gd-1_60"\
-l"boost_thread-vc120-mt-gd-1_60"\
-l"boost_timer-vc120-mt-gd-1_60"\