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;
}

運行結果:

相關文章
相關標籤/搜索