使用Boost线程的程序绝对没有任何意义

时间:2013-07-24 10:07:07

标签: multithreading boost mingw boost-thread

这个测试代码没问题,问题必须与构建它的方式有关:

#include <boost/thread.hpp>
#include <iostream>

void Wait(int seconds)
{
    boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void Thread()
{
    for (int i = 0; i < 5; ++i)
    {
        Wait(1);
        std::cout << i << std::endl;
    }
}

int main()
{
    std::cout << "Boost threads test:" << std::endl;
    boost::thread t(Thread);
    t.join();
}

这是CMakeLists.txt文件:

project(BoostThreadsTest)
cmake_minimum_required(VERSION 2.8)

find_package(Boost 1.46 REQUIRED COMPONENTS thread)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    message(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}")
else()
    message(WARNING "Boost headers/libraries not found.")
endif()

aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

target_link_libraries(${PROJECT_NAME}
    ${Boost_LIBRARIES}
    ${THREADING_LIBRARY}
    )

内置:

cmake -G "MinGW Makefiles" .
mingw32-make.exe

一切顺利,只有这个警告:

In file included from C:/boost/include/boost-1_48/boost/thread/win32/thread_data.hpp:12:0,
                 from C:/boost/include/boost-1_48/boost/thread/thread.hpp:15,
                 from C:/boost/include/boost-1_48/boost/thread.hpp:13,
                 from C:\Users\pietro.mele\projects\tests\buildSystem_test\BoostTest\BoostThreadsTest\BoostThreadsTest\main.cpp:4:
C:/boost/include/boost-1_48/boost/thread/win32/thread_heap_alloc.hpp:59:40: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int
)' declared as  dllimport: attribute ignored [-Wattributes]
C:/boost/include/boost-1_48/boost/thread/win32/thread_heap_alloc.hpp:69:39: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared
as  dllimport: attribute ignored [-Wattributes]

我得到了可执行文件,但是当我运行它时它什么也没做。甚至不打印“Boost线程测试:”字符串,但它不涉及多线程。没有错误产生。就像按下[return]键一样。

谢谢。

1 个答案:

答案 0 :(得分:2)

(只是粘贴我的评论,所以问题可以标记为已关闭)。

我怀疑找不到DLL。从cygwin执行exe时我看到过这种行为:根本没有任何事情发生。但是,当我双击Windows资源管理器中的exe时,我看到一个消息框抱怨缺少DLL:试试。如果这是原因,请调整PATH以启用DLL。


我不确定消息框被抑制的原因,如果我发现它(或者有人将其添加为注释)我将更新此答案。