版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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_systemchunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
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
<iostream>
#include
<boost/thread.hpp>
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
<iostream>
#include
<boost/thread.hpp>
using
namespace
std;
void
fun1(const
int
&id){cout
<<
"threads
id
"<<id
<<
endl;}
void
fun2(const
int
&id){cout
<<
"threads
id
"<<id
<<
endl;}
void
fun3(const
int
&id){cout
<<
"threads
id
"<<id
<<
endl;}
int
main()
{
boost::thread
t1(fun1,1); t1.join();
boost::thread
t2(fun2,2); t2.join();
boost::thread
t3(fun3,3); t3.join();
return
0;
}
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
threads
id
1
threads
id
2
threads
id
3
chunli@Linux:~/boost$线程的创建方式chunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
using
namespace
std;
void
fun1(const
int
&id)
{
cout
<<
"threads
id
"<<id
<<
endl;
}
struct
MyThread
{
void
operator()(const
int
&id)
{
cout
<<
"threads
id
"<<id
<<
endl;
}
void
fun(const
int
&id)
{
cout
<<
"threads
id
"<<id
<<
endl;
}
};
int
main()
{
boost::thread
t1(fun1,1);//自由函数
t1.join();
MyThread
mythread;
boost::thread
t2(mythread,2);//函数对象
t2.join();
boost::thread
t3(&MyThread::fun,mythread,3);
//成员函数
t3.join();
boost::thread
t4(MyThread(),4); //临时对象
t4.join();
boost::thread
t5(boost::ref(mythread),5);//对象引用
t5.join();
return
0;
}
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
threads
id
1
threads
id
2
threads
id
3
threads
id
4
threads
id
5
chunli@Linux:~/boost$线程的joinchunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
using
namespace
std;
void
fun1(const
int
&id){cout
<<
"threads
id
"<<id
<<
endl;}
int
main()
{
boost::thread
t1(fun1,1);
//t1.join();
cout
<<
"main
end!"
<<
endl;
return
0;
}
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
main
end!
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
main
end!threads
id
1
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
main
end!
threads
id
1
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
main
end!
threads
id
1chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
main
end!
threads
id
1
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
main
end!
threads
id
1
chunli@Linux:~/boost$
可以看出,如果没有join的等待,结果是不可预期的.加入join,回收线程chunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
using
namespace
std;
void
fun1(const
int
&id){cout
<<
"threads
id
"<<id
<<
endl;}
int
main()
{
boost::thread
t1(fun1,1);
t1.join();
cout
<<
"main
end!"
<<
endl;
return
0;
}
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
threads
id
1
main
end!
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
threads
id
1
main
end!
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
threads
id
1
main
end!
chunli@Linux:~/boost$
g++
main.cpp
-l
boost_thread
-l
boost_system
&&
./a.out
threads
id
1
main
end!
chunli@Linux:~/boost$线程中断chunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
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); errupt();
thread
t2(f2,
2);
thread
t3(f3,
3); errupt();
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
<iostream>
#include
<boost/thread.hpp>
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); errupt();
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$线程组chunli@Linux:~/桌面/qt_pro/01/untitled$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
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<<group.size()<<endl;
group.join_all();
}
chunli@Linux:~/桌面/qt_pro/01/untitled$
g++
main.cpp
-lboost_thread
-lboost_system
-Wall
&&
./a.out
fun1
4
fun1
fun2
fun1
chunli@Linux:~/桌面/qt_pro/01/untitled$
g++
main.cpp
-lboost_thread
-lboost_system
-Wall
&&
./a.out
fun1
fun1
fun1
4
fun2
chunli@Linux:~/桌面/qt_pro/01/untitled$boost线程的死锁chunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
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
<iostream>
#include
<boost/thread.hpp>
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
#"<<id<<":";
for(int
i=0;i<15;i++)
cout
<<
i<<'
';
cout
<<
endl;
m.unlock();
}
int
main()
{
thread
t1(function1,1);
t1.join();
thread
t2(function1,2);
t2.join();
thread
t3(function1,3);
t3.join();
thread
t4(function1,4);
t4.join();
thread
t5(function1,5);
t5.join();
thread
t6(function1,6);
t6.join();
}
chunli@Linux:~/boost$
g++
main.cpp
-lboost_thread
-lboost_system
-lpthread
&&
./a.out
thread
#1:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
thread
#2:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
thread
#3:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
thread
#4:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
thread
#5:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
thread
#6:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
chunli@Linux:~/boost$unique_lock锁,离开作用域自动释放chunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<list>
#include
<boost/thread.hpp>
using
namespace
std;
using
boost::thread;
boost::mutex
m;
int
k
=
0;
void
decrement()
{
boost::unique_lock<boost::mutex>
lock(m);
for(int
i
=
0;i<=100;++i)
{
k-=i;
}
cout
<<
"after
decrement
k="<<k
<<
endl;
}
void
increment()
{
boost::unique_lock<boost::mutex>
lock(m);
for(int
i
=
0;i<=100;++i)
{
k+=i;
}
cout
<<
"after
increment
k="<<k
<<
endl;
}
int
main()
{
boost::thread
t1(increment);
t1.join();
boost::thread
t2(decrement);
t2.join();
}
chunli@Linux:~/boost$
g++
main.cpp
-lboost_thread
-lboost_system
-lpthread
&&
./a.out
after
increment
k=5050
after
decrement
k=0
chunli@Linux:~/boost$unique_lock锁示例2,可以显式的释放锁chunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<vector>
#include
<boost/thread.hpp>
using
namespace
std;
using
boost::thread;
boost::mutex
m;
void
updateString()
{
boost::unique_lock<boost::mutex>
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$boost1次初始化chunli@Linux:~/boost$
cat
main.cpp
#include
<iostream>
#include
<boost/thread.hpp>
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
<iostream>
#include
<boost/thread.hpp>
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<boost::mutex>
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<boost::mutex>
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
<iostream>
#include
<boost/thread.hpp>
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<boost::mutex>
lockFrom(from.m,
boost::adopt_lock);
//
boost::unique_lock<boost::mutex>
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<boost::mutex>
lockFrom(from.m,
boost::adopt_lock);
boost::this_thread::sleep(boost::posix_time::seconds(1));
boost::lock_guard<boost::mutex>
lockTo(to.m,
boost::adopt_lock);
from.balance
-=
amount;
to.balance
+=
am
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度智能汽车抵押贷款服务合同
- 2025年度租房转租合同租赁物权属争议解决补充协议
- 二零二五年度社区车库租赁与便民服务合同
- 二零二五年度空调设备产品责任保险合同
- 2025年度专业级雇佣保姆全面照顾二岁以下婴幼儿服务协议书
- 2025年度离婚房产分割与财产分割纠纷预防合同
- 二零二五年度少儿艺术教育责任家长协议
- 2025年度城市隧道渣土运输及环保处理服务合同
- 科技驱动的宿舍楼内学生食堂设计
- 跨部门协同下的客户关系管理策略
- 九年级上册-备战2024年中考历史总复习核心考点与重难点练习(统部编版)
- 健康指南如何正确护理蚕豆病学会这些技巧保持身体健康
- 老客户的开发与技巧课件
- 2024建设工程人工材料设备机械数据分类和编码规范
- 26个英文字母书写(手写体)Word版
- GB/T 13813-2023煤矿用金属材料摩擦火花安全性试验方法和判定规则
- DB31 SW-Z 017-2021 上海市排水检测井图集
- 日语专八分类词汇
- GB/T 707-1988热轧槽钢尺寸、外形、重量及允许偏差
- GB/T 33084-2016大型合金结构钢锻件技术条件
- 高考英语课外积累:Hello,China《你好中国》1-20词块摘录课件
评论
0/150
提交评论