测试boost线程锁(多读一写lock、非阻塞lock)

// testBoostLock.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp> 

#include <iostream>
#include <vector>
using namespace std;
/*
Title:测试boost线程lock
Environment:VS2013Update3、boost 1.56
Tester:kagula
Date:2014-09-19
*/

int g_data = 0;

//ost::shared_mutex instance for multi read single write lock create!
boost::shared_mutex g_mutex;
//if you do not need single write multi read,etc only need exclusive lock
//you should reference boost::mutex and boost::lock_guard material!

void f_write(int id)
{
	while (true)
	{
		{
			//only one write
			boost::unique_lock<boost::shared_mutex> lock(g_mutex);
			cout << "f_write=>id=" << id << ",g_data=" << g_data++ << endl;
		}

		//if have any interrupt,break routine
		boost::this_thread::interruption_point();
		//wait one tenth second
		boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}//end for
}

void f_read(int id)
{
	while (true)
	{
		//multi read
		{
			boost::shared_lock<boost::shared_mutex> lock(g_mutex);
			cout << "f_read=>id=" << id << " ,g_data=" << g_data << endl;
		}
		//if have any interrupt,break routine
		boost::this_thread::interruption_point();
		//wait one tenth second
		boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}//end for


}

//test multi read single write
void testCase1()
{
	vector<boost::thread> vecT;
	const unsigned int count = 8;

	//start threads
	for (size_t i = 0; i < count; i++)
	{
		boost::thread t(f_write, i);
		boost::thread t2(f_read, count+i);

		vecT.push_back(boost::move(t));
		vecT.push_back(boost::move(t2));
	}

	//wait a second
	boost::detail::win32::Sleep(1000);

	//interrupt threads
	for (size_t i = 0; i < count*2; i++)
	{
		vecT[i].interrupt();
	}

	//wait until all threads stop
	for (size_t i = 0; i < count * 2; i++)
	{
		vecT[i].join();
	}
}//end func

//test try lock usage
void testCase2()
{
	/*
	notice:
	below snippet will no block, only illustrate how to use
	*/

	boost::unique_lock<boost::shared_mutex> lock(g_mutex,boost::defer_lock);

	if (lock.try_lock())
	{
		cout << "lock success, do something...." << endl;
		//after get lock,you must unlock it for other one use!
		lock.unlock();
	}//end if

	//lock twice boost will throw out a exception!

	//if the statement can not get lock in a second ,will return false!
	if (lock.timed_lock(boost::posix_time::milliseconds(1000)))
	{
		cout << "after get the lock, do something...." << endl;
		//after get lock,you must unlock it for other one use!
		lock.unlock();
	}
}//end func

int _tmain(int argc, _TCHAR* argv[])
{
	testCase1();
	testCase2();

	cout << "input any key to continue..." << endl;
	cin.get();

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值