boost库使用快速入门

 

     在一家公司待久了,接触到的确定不止一个产品/项目。就这样,接下来要维护一个使用了boost库产品。大致看了下,用的最多的仍是thread,mutex,bind,share_ptr,前三个linux系统都有对应的库文件,因此猜想windows平台采用boost的缘由吧linux

      安装bootstrap

      考虑到用的vs的版本跟别人的不同,须要下载源码本身编译一下。地址:http://www.boost.org/windows

       目前用的是1.50,因此在历史版本里面找(http://www.boost.org/users/history/函数

解压后点击bootstrap.bat文件,便生成bjam.exe文件this

考虑本机装了多个VS,因此打开了2010的命令行google

,转到boost_1_50_0路径下,输入b2回车,就开始运行b2.exe程序了。先是会把文件拷贝到c盘下,而后才生成库文件。不过,有几个库文件没生成,缘由不明。因此,就把缺失的库名用google搜索出来了(baidu没搜索到),其实托管源码的地方也有编译好的库文件,1.50版本最高编译到vs2010,地址:https://sourceforge.net/projects/boost/files/boost-binaries/1.50.0/spa

因此,不想动手的能够直接去sourceforge下载。之后若是要想在vs2012上使用boost库,就得把boost库的版本升到1.55以上,目前看到有其余的项目里用的是1.57的版本。.net

---------------------------------------------------------------命令行

检索了下论坛,其余人的作法以下:线程

       在命令行下,运行bootstrap.bat -vc10,而后输入bjam.exe。加入条件的话,格式以下:

bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static

 

       安装完了以后就是开发了

      开发

      在vs2010工程上右键属性->VC++目录,在对应的包含目录和库目录中加入对应的路径

这样就能够开始写代码了

使用thread的例子

#include <stdio.h>
#include <string>
#include <boost/thread/thread.hpp>
using namespace std;

void printing(void *p)  
{  
	int j = *(int *)p;
	cout<<j<<endl;
}

int main()
{
    int i = 9;
	boost::thread thrd(printing, &i);
    thrd.join();  
  
    system("pause");
    return 0;
}

在建立子线程后,对线程的操做可join,可detach。jion就是表示等待这个线程结束,至关于调用方阻塞了。detach,分离,即分离指定的线程,分离以后建立者就无法取得分离的线程属性。无论哪一种状况,都是被建立的线程结束了就会本身释放资源。若是在被建立的线程结束前,主进程退出了,那么线程也被迫结束。detach有点像父子分家,分家后老子无法管儿子。interrupt()等函数就无效了。而后,boost::thread跟linux下的pthread使用上区别蛮大的,linux里子线程能够调用jion和detach等,boost中不行。

 

使用锁的例子

#include <boost\thread\mutex.hpp>

boost::mutex io_mu;
void printing1(const string& str)  
{     
    try
    {
		io_mu.lock();
        for (int i = 0; i <= 5; i++)
	    {
		    cout<<str<<endl;
		    boost::this_thread::sleep(boost::posix_time::milliseconds(100));
            
        }
        io_mu.unlock();
    }
    catch(...)
    {
        io_mu.unlock();
    }
	
} 
int main()
{
    boost::thread thrdA(printing1, "hello");
	boost::thread thrdB(printing1, "boost");
	thrdA.join();
	thrdB.join();
    system("pause");
	return 0;
}
#include <stdio.h>
#include <string>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>


boost::mutex io_mu;
void printing1(const string& str)  
{  
    for (int i = 0; i <= 5; i++)
	{
		//unique_lock<mutex>
		boost::mutex::scoped_lock lock(io_mu); //锁
		cout<<str<<endl;
		boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}
} 

int main()
{
    boost::thread thrdA(printing1, "hello");
	boost::thread thrdB(printing1, "boost");
	thrdA.join();
	thrdB.join();
    system("pause");
	return 0;
}

 scoped_lock即unique_lock<mutex>的对象在出了做用域后就自动解锁了,也就是析构函数内会解锁。so,要等for结束后才解锁,因而看到结果是串行的。

boost::unique_lock<T>,boost::shared_lock<T>,其中unique_lock为独占锁,shared_lock为共享锁。常见的例子就是读和写,通常读用共享锁,写用独占锁。多个读线程能够同时进去,不会相互影响。

若是要两个线程交替运行,把代码改为以下

void printing1(const string& str)  
{  
    for (int i = 0; i <= 5; i++)
	{
		io_mu.lock();
		cout<<str<<endl;
		io_mu.unlock();
		boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}
}

运行结果以下:

 

   共享锁

有时候会对一个文件进行读写操做,一个线程进行写,多个线程进行读。写的时候须要用惟一锁,当这个线程操做文件的时候,就不容许其余线程进行操做了。而后一个读线程操做文件的时候,其余的读线程也是能够读的。

boost::mutex io_mu;
boost::shared_mutex g_mutex;
int g_data = 0;

// 惟一锁
void printingR(const string& str)  
{  
    for (int i=0;i<20; i++)
	{
		boost::shared_lock<boost::shared_mutex> rlock(g_mutex);
		cout << "Rstr=" << str << "  g_data=" << g_data << endl;
		
		boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}
}

// 共享锁
void printingW(const string& str)  
{  
    for (int i=0;i<20; i++)
	{
		boost::unique_lock<boost::shared_mutex> wlock(g_mutex);
		cout << "Wstr=" << str << "  g_data=" << g_data++ << endl;
		//typedef boost::shared_lock<boost::shared_mutex> readLock;
		boost::this_thread::sleep(boost::posix_time::milliseconds(10));
	}	
}


int main()
{
    boost::thread thrdR(printingR, "read1");
	boost::thread thrdW(printingW, "write1");
	boost::thread thrdR2(printingR, "read2");
	boost::thread thrdW2(printingW, "write2");
	thrdW.join();
	thrdR.join();
	thrdR2.join();
	thrdW2.join();
    return 0;
}

运行结果:

相关文章
相关标签/搜索