2016-03-14 56 views
1

我正在將應用程序移植到Windows上,並發現當跨越DLL邊界中斷boost :: thread時,線程不會中斷。當跨越DLL邊界時,Boost線程中斷不起作用

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。親(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

此問題是由於沒有動態鏈接以提升線程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"\