boost、date_time和thread库都编译了但使用thread库时没有办法编译!
提示:
1>BootThread.obj : error LNK2019: 无法解析的外部符号 "void __cdecl boost::tss_cleanup_implemented(void)" (?tss_cleanup_implemented@boost@@YAXXZ),该符号在函数 "void __cdecl boost::`anonymous namespace'::create_current_thread_tls_key(void)" (?create_current_thread_tls_key@?A0x60de1d11@boost@@YAXXZ) 中被引用
1>C:\Users\ZZD\documents\visual studio 2010\Projects\BootThread\Debug\BootThread.exe : fatal error LNK1120: 1 个无法解析的外部命令
解决方案:
#define BOOST_HAS_WINTHREADS
#define BOOST_THREAD_BUILD_DLL
定义这两个宏
#define BOOST_DATA_TIME_SOURCE
#define BOOST_THREAD_NO_LIB
#define BOOST_HAS_WINTHREADS
#define BOOST_THREAD_BUILD_DLL
#include <string>
#include <iostream>
#include "boost/thread.hpp"
//#ifdef _MSC_VER
//extern "C" void tss_cleanup_implemented(void){}
#include "libs/thread/src/win32/thread.cpp"
#include "libs/thread/src/win32/tss_dll.cpp"
#include "libs/thread/src/win32/tss_pe.cpp"
//#endif
using namespace boost;
using namespace std;
boost::mutex io_mu;
void printing(int& x, const string& str)
{
for(int Index = 0; Index < 5; ++Index)
{
mutex::scoped_lock lock(io_mu);
cout<< str << ++x<< endl;
this_thread::sleep(posix_time::millisec(100));
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int x; //原子操作的计数器
//使用临时thread对象启动线程
thread(printing,boost::ref(x),"hello"); //向函数传递多个参数
thread(printing,boost::ref(x),"boost"); //使用ref库传递引用
this_thread::sleep(posix_time::seconds(2)); //等待2秒钟
return 0;
}