zthread学习 实例十四 信号量机制
来源:互联网 发布:手机编程前景 编辑:程序博客网 时间:2024/04/28 17:11
ZThread库提供了另外一种同步机制——信号量,它定义了两种信号量:Semaphore(带上界) 和 CountingSemaphore(不带上界),都类似于传统的信号量,下面例子说明了其用法:
- #include "stdafx.h"
- #include "Display.h"
- #include <iostream>
- using namespace std;
- using namespace ZThread;
- class Reads : public Runnable
- {
- public:
- Reads(CountedPtr<Semaphore>/*CountedPtr<CountingSemaphore>*/& aSemaphore, CountedPtr<Display>& disp, int idn = -1):
- id(idn), pSemaphore(aSemaphore), pDisplay(disp){}
- void run()
- {
- try
- {
- while (!Thread::interrupted())
- {
- //信号量减1
- pSemaphore->acquire();
- output(" ,Jarry Reading...");
- Thread::sleep(1000);
- //信号量增1
- pSemaphore->release();
- }
- }
- catch (Interrupted_Exception& e)
- {
- cerr << "Jarry " << e.what() << endl;
- }
- catch (InvalidOp_Exception& e) //越界,结束该线程
- {
- cerr << "Jarry " << e.what() << endl;
- return;
- }
- }
- //同步输出
- void output(string str)
- {
- ostringstream os;
- os << *this << " : " << str <<endl;
- pDisplay->OutPut(os);
- }
- friend ostream& operator<< (ostream& os, Reads& reads)
- {
- return os << "id: " << reads.id << " Count = " << reads.pSemaphore->count();
- }
- private:
- int id;
- CountedPtr<Semaphore> pSemaphore;
- //CountedPtr<CountingSemaphore> pSemaphore;
- CountedPtr<Display> pDisplay;
- };
- int main()
- {
- //创建一个count为5信号量,当count计数达到上界时,就会抛出InvalidOp_Exception异常
- CountedPtr<Semaphore> pSemaphore(new Semaphore(5, 2));
- //类似Semaphore,只是没有一个上界,不会抛出InvalidOp_Exception异常
- //CountedPtr<CountingSemaphore>pSemaphore(new CountingSemaphore(5));
- CountedPtr<Display> display(new Display);
- ThreadedExecutor executor;
- for (int i = 0; i < 20; i ++)
- {
- //这样测试是为了有在创建线程之前有线程执行,并且能促使信号量增1
- executor.wait(1500);
- executor.execute(new Reads(pSemaphore, display, i));
- }
- cin.get();
- executor.interrupt();
- cin.get();
- return 0;
- }
运行结果:
其中同步显示类,好像还没有贴出来,暂且贴在些吧:
- #ifndef DISPLAY_H
- #define DISPLAY_H
- #include <iostream>
- #include <sstream>
- using namespace ZThread;
- using namespace std;
- class Display
- {
- public:
- void OutPut(ostringstream& os)
- {
- Guard<Mutex> g(Lock);
- cout<<os.str();
- }
- private:
- Mutex Lock;
- };
- #endif
0 0
- zthread学习 实例十四 信号量机制
- zthread学习 实例十一 信号量机制
- zthread学习 实例一
- zthread学习 实例二
- zthread学习 实例四 让步、休眠、优先级
- zthread学习 实例六 访问控制
- zthread学习 实例七 线程本地存储
- zthread学习 实例四 让步、休眠、优先级
- zthread学习 实例六 访问控制
- zthread学习 实例七 线程本地存储
- zthread学习 实例十二 ZThread库基本结构的规纳
- zthread学习 实例十五ZThread库基本结构的规纳
- zthread学习 实例三 使用Executor器来执行线程
- zthread学习 实例五 非任务对象的生存期
- zthread学习 实例十 线程间的协助(一)
- zthread学习 实例十 线程间的协助(二)
- zthread学习 实例五 非任务对象的生存期
- zthread学习 实例十 线程间的协助(一)
- zthread学习 实例十三 线程间的协助(四)——读者、写者
- 记一次监控问题解决历程
- 菜鸟学习之路——SQLite数据库的学习
- HTML语法大全
- 【listview精深系列2】listview基础讲解
- zthread学习 实例十四 信号量机制
- 条款15:在资源管理类中提供对原始资源的访问
- SPRING技术内幕:深入解析SPRING架构与设计原理(第2版)-笔记(三)AOP概念
- hinernate 一对一、一对多、多对多举例
- My97DatePicker 输入框 的用法
- zthread学习 实例十五ZThread库基本结构的规纳
- Impala 教程
- java.io.FileNotFoundException: /mnt/sdcard/yuan.txt (Permission denied) at org.apache.harmony.luni.p
- java笔记--关于线程同步(5种同步方式)