boost开发
在线教程
多线程编译测试
#include <boost/thread.hpp> #include<iostream> int main() { std::cout <<boost::thread::hardware_concurrency() <<'\n' <<boost::thread::physical_concurrency() <<'\n'; return 0; } /* g++ -o 20191118.o 20191118.c -l boost_thread -l boost_system -l pthread */
线程锁
boost::mutex mtx void my_thread() { .... boost::lock_guard<boost::mutex>lock(mtx); //lock_guard在构造函数里加锁,在析构函数里解锁。 .... }
共享内存编译测试
#include<boost/interprocess/shared_memory_object.hpp> #include<iostream> int main() { boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create,"Highscore",boost::interprocess::read_write); shdmem.truncate(1024); boost::interprocess::offset_t size; if(shdmem.get_size(size))std::cout<<size<<'\n'; return 0; } /* g++ -o test.o test.c -l rt */