一.main.cpp函数的实现,如下:
#include <iostream>
#include <boost/thread/mutex.hpp>
template<typename T>
class Counter:public boost::noncopyable{
private:
T t;
boost::mutex mu;
public:
Counter(T _t = T()):t(_t){}
T operator++(){
boost::mutex::scoped_lock lock(mu);
return ++t;
}
operator T(){
std::cout<<"inside T()"<<std::endl;
return t;
}
};
int main(int argc,char** argv){
typedef Counter<int> count_int;
count_int x(9);
std::cout<<++x<<std::endl;
return 0;
}
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8222322
二.makefile文件的编写,如下:
SRC = $(wildcard \
./*.cpp \
)
CC = g++
REFLAGS = -O2 -g
EXEC = ./bin/main
CFLAGS = -I/usr/local/include
LDFLAGS = -L./lib "-Wl,-rpath,./lib"
LIBS = -lboost_thread -lpthread -lrt -ldl
REL_OBJS = $(SRC:%.cpp=%.o)
rel:$(REL_OBJS)
$(CC) $(RELFLAGS) $(LDFLAGS) $^ $(LIBS) -o $(EXEC)
%.o:%.cpp
$(CC) -c $(RELFLAGS) $(CFLAGS) $< -o $@
clean:
rm -f $(REL_OBJS) $(EXEC)
.PHONY:rel
三.可能会出现的问题,如下:
undefined reference to `boost::thread_resource_error::thread_resource_error()'
说明:该问题其实很好解决,当然也得看具体情况,这里略去。
参考文献:boost库完全开发指南,罗剑锋 著
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8222322