boost 使用(四)
来源:互联网 发布:软件开发绩效考核指标 编辑:程序博客网 时间:2024/05/16 19:02
使用boost中的线程库中的锁,先介绍简单的锁的用方法,会api锁的人可以跳过了,和api的没有区别的,新手请继续。先贴上代码:
#include <boost/thread.hpp> #include <iostream> void wait(int seconds){boost::this_thread::sleep(boost::posix_time::seconds(seconds));}boost::mutex mutex;void thread(){for (int i = 0; i < 5; ++i){wait(1);mutex.lock();std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;mutex.unlock();}}int main(){boost::thread t1(thread);boost::thread t2(thread);t1.join();t2.join();system("pause");}
你会发现没有太多新加的内容,只有mutex.lock和 mutex.unlock;这两个新的函数,这个是加锁和解锁两个操作。好了这就是这次带来的内容。这个操作可以保证多线程中改变同样的地址空间的时候不会发生悲剧。保证线程安全。好了学过操作系统的你一定懂。
1 0
- boost 使用(四)
- boost log库使用四
- boost log库学习使用四(关于logger)
- 引领Boost(四)(Boost::smart_ptr)
- 引领Boost(四)(Boost::smart_ptr)
- Boost.Asio基础(四)
- boost使用(二)
- boost使用(五)
- boost使用(七)
- boost使用(八)
- boost使用(九)
- [Boost.asio] 深入linux网络编程(四):使用asio搭建商用服务器
- Boost 学习 (四)Conversion 库
- boost中的bind的四种使用环境
- vs2010 使用boost库 (二) 使用Boost libraries
- boost的使用(一)
- Boost-asio之四
- 使用 Boost
- android 画廊实现异步加载
- 配置SSH所需jar包地址 + SSH实现分页功能+ SSH基本框架配置
- 十三 void bzero(void *s, int n);
- win7安装oracle10g时网络配置错误
- Java之排序(冒泡排序、选择排序)
- boost 使用(四)
- 十四 setsockopt函数使用
- 复选框单选框与文字对齐问题
- session之此session非彼session
- 十五 send,recv,sendto,recvfrom
- No JVM could be found on your system. Please define EXE4J_JAVA_HOME to point to an installed 32-bit
- 十六 sockaddr_in , sockaddr , in_addr区别
- (六)Unity3d第五次面试
- android APK反编译