gpt4 book ai didi

c++ - Boost 线程中断在跨越 DLL 边界时不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:14:17 24 4
gpt4 key购买 nike

我正在将一个应用程序移植到 Windows 上,发现当跨 DLL 边界中断 boost::thread 时,线程不会被中断。

boost 库是静态链接的,并使用调试运行时和链接到共享运行时库的多线程支持构建。/MDd

该项目是使用 msvc12 (2013) 编译的(与编译 boost 的编译器相同)。

我做了一个简短的示例项目来演示这个问题:

测试.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;
}

测试库.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";
}

测试库.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

测试库.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

最佳答案

这个问题是由于没有动态链接到 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"\

关于c++ - Boost 线程中断在跨越 DLL 边界时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35978572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com