这个测试代码是可以的,问题一定出在我构建它的方式上:
#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 threads test:”字符串。不会产生错误。就像按下 [return] 键一样。
谢谢你。