(十一)boost库之多线程间通信
(十一)boost库之多线程间通信
1、互斥锁
在编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性。每个对象都对应于一个可称为" 互斥锁" 的标记,这个标记用来保证在任一时刻,只能有一个线程访问该对象。
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
int g_num = 0;
boost::mutex mu; //定义互斥锁对象
int Func(int nCount)
{
for (int i = 0; i < nCount; i++)
{
boost::mutex::scoped_lock lock(mu); //对共享数据进行操作,需加锁
g_num++;
cout << __FUNCTION__ << ": " << g_num << endl;
}
return g_num;
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread th1(Func, 100);
boost::thread th2(Func, 200);
th1.join();
th2.join();
return 0;
}
2、读写锁
boost::shared_mutex rw_mu; //定义读写锁
int Write(int nCount)
{
for (int i = 0; i < nCount; i++)
{
boost::unique_lock<boost::shared_mutex> lock(rw_mu); //加唯一锁
g_num++;
cout << __FUNCTION__ << ": " << g_num << endl;
}
return g_num;
}
void Read(int nCount)
{
for (int i = 0; i < nCount; i++)
{
boost::shared_lock<boost::shared_mutex> lock(rw_mu); //加共享锁
cout << __FUNCTION__ << ": " << g_num << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread th1(Write, 100);
boost::thread th2(Read, 100);
boost::thread th3(Read, 100);
th1.join();
th2.join();
th3.join();
return 0;
}
3、条件量
条件量相对于互斥锁和读写锁来说,并不是那么好理解,简单点说,条件变量就是用于等待某个条件被触发,但为什么要配合锁使用呢,因为我们的等待不能是干等,那样可能会出现死锁。
如线程A负责添加任务到队列,线程B负责处理队列中的任务,队列就是两个线程的共享资源,使用前必须加锁,但如果B线程加锁后,发现队列中没有数据,然后等待,A线程准备添加任务时,发现
锁已经被占用,于是就没法添加任务,就形成了死锁。但如果我等待时,释放锁资源,A线程就能正常添加任务,完成后通知B线程可以处理了,那么整个流程就畅通无阻了,这就是条件量的作用。
#include <queue>
boost::mutex g_ioMutex; //输出控制锁
template<typename T>
class CMsgQueue
{
public:
CMsgQueue(size_t n):m_nCapacity(n)
{
}
void Push(const T& val)
{
{
boost::mutex::scoped_lock lock(m_mu); //加锁
while(m_val.size() == m_nCapacity) //队列已满
{
{
boost::mutex::scoped_lock lock(g_ioMutex);
cout << "队列已满" << endl;
}
m_condPush.wait(m_mu); //等待,将暂时的解锁
}
m_val.push(val); //添加数据到队列
}
m_condPop.notify_one(); //通知读线程
}
void Pop(T& val)
{
{
boost::mutex::scoped_lock lock(m_mu); //加锁
while(m_val.size() == 0) //队列为空
{
{
boost::mutex::scoped_lock lock(g_ioMutex);
cout << "队列为空" << endl;
}
m_condPop.wait(m_mu); //等待可读,
}
val = m_val.front(); //读取数据
m_val.pop();
}
m_condPush.notify_one(); //通知写线程
}
private:
queue<T> m_val; //队列
int m_nCapacity; //队列最大容量
boost::condition_variable_any m_condPush; //写入条件量
boost::condition_variable_any m_condPop; //读取条件量
boost::mutex m_mu; //互斥锁
};
CMsgQueue<int> g_numQueue(10);
void FuncA(int nCount)
{
for (int i = 0; i < nCount; i++)
{
{
boost::mutex::scoped_lock lock(g_ioMutex);
cout << __FUNCTION__ << " Put " << i << endl;
}
g_numQueue.Push(i);
}
}
void FuncB(int nCount)
{
for (int i = 0; i < nCount; i++)
{
int val;
g_numQueue.Pop(val);
boost::mutex::scoped_lock lock(g_ioMutex);
cout << __FUNCTION__ << " Get " << val << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread th1(FuncA, 50);
boost::thread th2(FuncB, 20);
boost::thread th3(FuncB, 30);
th1.join();
th2.join();
th3.join();
return 0;
}
在多线程程序中,锁的使用需要特别的小心,比如,我们将FuncA稍微改一下:
void FuncA(int nCount)
{
for (int i = 0; i < nCount; i++)
{
boost::mutex::scoped_lock lock(g_ioMutex);
cout << __FUNCTION__ << " Put " << i << endl;
g_numQueue.Push(i);
}
}
如果改成这样,程序将陷入死锁,我们轻轻松松就制造了一个死锁案例。
A线程占用了输入锁,那么B线程的Pop函数将一直在获取输入锁的地方等待,但它已经占用了m_mu锁,A线程也就只能一直在等待m_mu,故形成了死锁。
(十一)boost库之多线程间通信的更多相关文章
- (十二)boost库之多线程高级特性
(十二)boost库之多线程高级特性 很多时候,线程不仅仅是执行一些耗时操作,可能我们还需要得到线程的返回值,一般的处理方法就是定义一个全局状态变量,不断轮训状态,就如我目前维护的一个项目,全局变量定 ...
- (十)boost库之多线程
(十)boost库之多线程 1.创建线程 使用boost库可以方便的创建一个线程,并提供最多支持9个参数的线程函数,相对于void*来说,方便了很多,创建线程主要提供了一下3种方式: 线程库头文件:# ...
- Java 多线程间通信
JDK 1.5 以后, 将同步和锁封装成了对象, 并将操作锁的隐式方法定义到了该对象中, 将隐式动作变成了显示动作. Lock 接口 Lock 接口, 位于 java.util.concurrent. ...
- Java多线程间通信-解决安全问题、等待唤醒机制
/*1.增加一个知识点一个类怎么在所有的类中,让其它类来共同修改它的数据呢?可以用单例设计模式可以用静态可以在其它类中做一个构造函数,接受同一个对象,这样就可以实现对象 2.状态选择可以用数字0 1 ...
- 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析和开发示例
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
- java 多线程间通信(二)
传统的线程通信 Object提供了三个方法wait(), notify(), notifyAll()在线程之间进行通信,以此来解决线程间执行顺序等问题. wait():释放当前线程的同步监视控制器,并 ...
- 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
- java 多线程间通信(一)
synchronized同步 package com.test7; public class Run { public class MyObject { private int a; public M ...
- boost库:多线程
1.线程管理 最重要的一个类是boost::thread,是在boost/thread.hpp里定义的,用来创建一个新线程. #include <boost/thread.hpp> #in ...
随机推荐
- python学习资料
http://woodpecker.org.cn/diveintopython/ http://www.cnblogs.com/txw1958/archive/2012/12/10/A_Byte_of ...
- scrollbarsstyle
android:scrollbarStyle属性及滚动条和分割线覆盖问题 本文主要介绍android view的android:scrollbarStyle属性意义android:scrollbarS ...
- grep搜索当前目录和递归搜索子目录中文本文件的特定pattern
一般在windows上文本编辑器notepad++,UE这些都有这些功能,Linux下就换了一种方式,用grep来完成文件中信息查找的方式. grep -R -n --include="*. ...
- Hamming code
Also known as (7,4) code,7 trainsmitted bits for 4 source code. TRANSMIT The transmitted procedure c ...
- 格而知之7:我所理解的Runtime(2)
消息发送(Messaging) 8.以上便是runtime相关的一些数据结构,接下来我们回看一开始的疑问: objc_msgSend()函数在执行的过程中是如何找到对应的类,找到对应的方法实现的呢? ...
- Android应用程序请求SurfaceFlinger服务渲染Surface的过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7932268 在前面一篇文章中,我们分析了And ...
- Unity 之圆环算法
首先我们要明白圆环生成的原理,其实说白了并不是圆环,而是圆.因为我们使用的预制物体时Cube(物体本身是有大小的)难免会有发生实物的折叠看起来给人的感觉是圆环而已. 1.1 几何中我们要画一个圆,因为 ...
- Starting httpd:Could not reliably determine the server's fully qualified domain name
#service httpd start #Starting httpd: httpd: Could not reliably determine the server's fully qualifi ...
- locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
安装好CentOS后,第一次进入系统使用locate命令,结果出现:locate: can not stat () `/var/lib/mlocate/mlocate.db': No such fil ...
- git-svn 的使用
从 SVN 克隆代码 git svn clone https://192.168.1.3/svn/project-name git-svn 初始化 git svn init (svn remote ...