网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++中Boost多线程、线程同步的示例分析

小编给大家分享一下C++中Boost多线程、线程同步的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联专注于新绛网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供新绛营销型网站建设,新绛网站制作、新绛网页设计、新绛网站官网定制、小程序定制开发服务,打造新绛网络公司原创品牌,更为您提供新绛网站排名全网营销落地服务。

线程的创建 boost_thread,boost_system
多线程的创建
线程的参数传递
线程的创建方式
线程的join
加入join,回收线程

线程中断
线程中断2,
线程组
boost 线程的死锁
boost 线程递归锁
线程互斥锁,线程同步
unique_lock 锁,离开作用域自动释放
unique_lock 锁 示例 2,可以显式的释放锁
boost 1次初始化
boost 条件变量
boost 线程锁,一个账户往另外一个账户转钱案例
boost upgrade_lock

知识背景:

理解什么是线程,什么是进程,区别是什么,如何使用多进程多线程

线程的创建 boost_thread,boost_system

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;

void fun()
{
	cout << "Hello Boost threads !" << endl;
}

int main() 
{
	boost::thread t1(fun);
	t1.join();
	
	return 0;
}
chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
Hello Boost threads !
chunli@Linux:~/boost$

多线程的创建

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;

void fun1(){cout << "Hello Boost threads 1!" << endl;}
void fun2(){cout << "Hello Boost threads 2!" << endl;}
void fun3(){cout << "Hello Boost threads 3!" << endl;}

int main() 
{
	boost::thread t1(fun1);	t1.join();
	boost::thread t2(fun2);	t2.join();
	boost::thread t3(fun3);	t3.join();
	
	return 0;
}
chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
Hello Boost threads 1!
Hello Boost threads 2!
Hello Boost threads 3!
chunli@Linux:~/boost$

线程的参数传递

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;
void fun1(const int &id){cout << "threads id "<

线程的创建方式

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;

void fun1(const int &id)
{
	cout << "threads id "<

线程的join

C++中Boost多线程、线程同步的示例分析

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;

void fun1(const int &id){cout << "threads id "<

加入join,回收线程

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;

void fun1(const int &id){cout << "threads id "<

线程中断

C++中Boost多线程、线程同步的示例分析

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;
using boost::thread;

void f1(const int& id) 
{
	cout << "thread #" << id << ": started" << endl;
	boost::system_time const timeout = boost::get_system_time()+ boost::posix_time::seconds(3);
	thread::sleep(timeout);//sleep不会放弃时间片
	cout << "thread #" << id << ": ended" << endl;
}

void f2(const int& id) 
{
	cout << "thread #" << id << ": started" << endl;
	thread::yield();//预定义中断点.主动放弃时间片
	cout << "thread #" << id << ": ended" << endl;
}

void f3(const int& id) 
{
	cout << "thread #" << id << ": started" << endl;
	boost::this_thread::interruption_point();//预定义中断点
	cout << "thread #" << id << ": ended" << endl;
}

int main() 
{
	thread t1(f1, 1);	t1.interrupt();
	thread t2(f2, 2);
	thread t3(f3, 3);	t3.interrupt();

	t1.join();
	t2.join();
	t3.join();
}

chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
thread #2: started
thread #1: started
thread #3: started
thread #2: ended
chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
thread #1: started
thread #3: started
thread #2: started
thread #2: ended
chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
thread #thread #2: started1: started
thread #3: started

thread #2: ended
chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
thread #3: started
thread #1: started
thread #2: started
thread #2: ended
chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
thread #2: started
thread #3: started
thread #thread #2: ended
1: started
chunli@Linux:~/boost$ 
只有2线程不会被打断

线程中断2,

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;
using boost::thread;

void print(const int& id) 
{
	boost::this_thread::disable_interruption di;//创建一个不可被打断的对象
	cout << boost::this_thread::interruption_enabled() << endl;
	cout << "thread #" << id << ": ";
	//boost::this_thread::sleep(boost::posix_time::seconds(2));
	boost::system_time const timeout = boost::get_system_time() + boost::posix_time::seconds(2);
	thread::sleep(timeout);

	for (int i = 1; i < 11; ++i)
	{
		cout << i << ' ';
	}
	cout << endl;

	boost::this_thread::restore_interruption ri(di);//到这里,对象不可被打断
	cout << boost::this_thread::interruption_enabled() << endl;
	//实际上,是可以被打断
}

int main() 
{
	//线程还没有运行结束,叫被打断
	thread t1(print, 1);
	thread t2(print, 2);
	thread t3(print, 3);	t3.interrupt();

	t1.join();
	t2.join();
	t3.join();
}

chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
0
thread #1: 0
thread #3: 0
thread #2: 1 2 3 4 5 6 7 8 9 10 
1
1 2 3 4 5 6 7 8 9 10 
1
1 2 3 4 5 6 7 8 9 10 
1
chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 
0
thread #1: 0
thread #2: 0
thread #3: 1 2 3 4 5 6 7 8 9 10 
1
1 2 3 4 5 6 7 8 9 10 
1
1 2 3 4 5 6 7 8 9 10 
1
chunli@Linux:~/boost$

C++中Boost多线程、线程同步的示例分析

线程组

chunli@Linux:~/桌面/qt_pro/01/untitled$ cat main.cpp 
#include 
#include 
using namespace std;
using boost::thread;
void f1(){    cout << "fun1 " << endl;}
void f2(){    cout << "fun2 " << endl;}

int main()
{
    boost::thread_group group;
    for(int i = 0;i<3;++i)
    {
        group.create_thread(f1);
    }
    group.add_thread(new boost::thread(f2));
    cout<

boost 线程的死锁

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;
using boost::thread;

boost::mutex m;

void function1()
{
    m.lock();
    cout << "function 1 \n";
    m.unlock();
}

void function2()
{
    m.lock();
    cout << "function 2 \n";
    function1();
    m.unlock();
}


int main()
{
    thread t1(function1);   t1.join();
    thread t2(function2);   t2.join();
}



chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system&& ./a.out 
function 1 
function 2 
^C
chunli@Linux:~/boost$

boost 线程递归锁

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;
using boost::thread;

boost::recursive_mutex m;

void function1()
{
    m.lock();
    cout << "function 1 \n";
    m.unlock();
}

void function2()
{
    m.lock();
    cout << "function 2 \n";
    function1();
    m.unlock();
}


int main()
{
    thread t1(function1);   t1.join();
    thread t2(function2);   t2.join();
}



chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out 
function 1 
function 2 
function 1 
chunli@Linux:~/boost$

线程互斥锁,线程同步

boost::mutex m;

void function1(int id)
{
    m.lock();
    cout <<"thread #"<

unique_lock 锁,离开作用域自动释放

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 

#include 
using namespace std;
using boost::thread;

boost::mutex m;
int k = 0;

void decrement()
{
    boost::unique_lock lock(m);
    for(int i = 0;i<=100;++i)
    {
        k-=i;
    }
    cout << "after decrement k="< lock(m);
    for(int i = 0;i<=100;++i)
    {
        k+=i;
    }
    cout << "after increment k="<

unique_lock 锁 示例 2,可以显式的释放锁

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
#include 
using namespace std;
using boost::thread;

boost::mutex m;

void updateString()
{
    boost::unique_lock lock(m);//lock
    lock.unlock();//unlock
    lock.lock();
}

int main()
{
    thread t1(updateString);    t1.join();
    thread t2(updateString);    t2.join();
}






chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out 
chunli@Linux:~/boost$

boost 1次初始化

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 

using namespace std;
using boost::thread;
boost::once_flag once = BOOST_ONCE_INIT; // 注意这个操作不要遗漏了
void func() {
	cout << "Will be called but one time!" << endl;
}
void threadFunc() {
	//    func();
	boost::call_once(&func, once);
}

int main() {
	boost::thread_group threads;
	for (int i = 0; i < 5; ++i)
		threads.create_thread(&threadFunc);
	threads.join_all();
}

chunli@Linux:~/boost$ g++ main.cpp  -lboost_thread -lboost_system && ./a.out 
Will be called but one time!
chunli@Linux:~/boost$

boost 条件变量

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 
using namespace std;
using boost::thread;

boost::condition_variable cond; // 关联多个线程的条件变量
boost::mutex m; // 保护共享资源 k 的互斥体
int k = 0; // 共享资源

void f1(const int& id) 
{
	boost::unique_lock lock(m);
	while (k < 5) 
	{
		cout << "thread #" << id << ": k < 5, waiting ..." << endl;
		cond.wait(lock); // #1
	}
	cout << "thread #" << id << ": now k >= 5, printing ..." << endl;
}

void f2(const int& id) 
{

	boost::unique_lock lock(m);
	cout << "thread #" << id << ": k will be changed ..." << endl;
	k += 5;
	cond.notify_all(); // #2 不需lock
}

int main() {
	// 如果f2()中是 cond.notify_one(),结果?
	boost::thread t1(f1, 1);
	boost::thread t2(f1, 2);
	boost::thread t3(f2, 100);

	t1.join();
	t2.join();
	t3.join();
}

chunli@Linux:~/boost$ g++ main.cpp  -lboost_thread -lboost_system && ./a.out 
thread #1: k < 5, waiting ...
thread #2: k < 5, waiting ...
thread #100: k will be changed ...
thread #1: now k >= 5, printing ...
thread #2: now k >= 5, printing ...
chunli@Linux:~/boost$

boost 线程锁,一个账户往另外一个账户转钱案例

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 

using namespace std;
using boost::thread;

class Account {
	boost::mutex m;
	double balance;
	public:
	Account() :
		balance() {
		}

	Account(const double& bal) :
		balance(bal) {
		}

	double getBalance() const {
		return balance;
	}

	friend void transfer(Account& from, Account& to, double amount);
};


//// version 3: OK (使用lock() 和 unique_lock)
//void transfer(Account& from, Account& to, double amount) {
//    boost::lock(from.m, to.m);
//    boost::unique_lock lockFrom(from.m, boost::adopt_lock);
//    boost::unique_lock lockTo(to.m, boost::adopt_lock);
//
//    from.balance -= amount;
//    to.balance += amount;
//}

// version 2: OK (使用lock() 和 lock_guard)
void transfer(Account& from, Account& to, double amount) {
	boost::lock(from.m, to.m);
	boost::lock_guard lockFrom(from.m, boost::adopt_lock);
	boost::this_thread::sleep(boost::posix_time::seconds(1));
	boost::lock_guard lockTo(to.m, boost::adopt_lock);
	from.balance -= amount;
	to.balance += amount;
}

// version 1: 可能造成死锁
//void transfer(Account& from, Account& to, double amount) {
//    boost::lock_guard lockFrom(from.m); // #1
//    boost::this_thread::sleep(boost::posix_time::seconds(1));
//    boost::lock_guard lockTo(to.m); // #2
//    from.balance -= amount;
//    to.balance += amount;
//}

int main() {
	Account a1(1200.00);
	Account a2(300.00);

	boost::thread t1(transfer, boost::ref(a1), boost::ref(a2), 134.85);
	boost::thread t2(transfer, boost::ref(a2), boost::ref(a1), 100.30);
	t1.join();
	t2.join();

	cout << "Balance of a1: " << a1.getBalance() << endl;
	cout << "Balance of a2: " << a2.getBalance() << endl;
}

chunli@Linux:~/boost$ g++ main.cpp  -lboost_thread -lboost_system -lpthread && ./a.out 
Balance of a1: 1165.45
Balance of a2: 334.55
chunli@Linux:~/boost$

boost upgrade_lock

chunli@Linux:~/boost$ cat main.cpp 
#include 
#include 

using namespace std;
using boost::thread;
boost::shared_mutex m;
int k = 1;

void f(int id) {
	boost::upgrade_lock lock(m);
	cout << "thread #" << id << ": " << k << endl;
	if (k < 6) {
		// boost::unique_lock lock2(boost::move(lock)); // alternate:
		boost::upgrade_to_unique_lock lock2(lock);
		k += 3;
	}
}

int main() {
	boost::thread t1(f, 1);
	boost::thread t2(f, 2);
	boost::thread t3(f, 3);

	t1.join();
	t2.join();
	t3.join();
}

chunli@Linux:~/boost$ g++ main.cpp  -lboost_thread -lboost_system -lpthread && ./a.out 
thread #2: 1
thread #1: 4
thread #3: 7
chunli@Linux:~/boost$

以上是“C++中Boost多线程、线程同步的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享标题:C++中Boost多线程、线程同步的示例分析
转载来源:http://bjjierui.cn/article/jsodjh.html

其他资讯