#include <pthread.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std; class RWLock { private :
pthread_mutex_t cnt_mutex;
pthread_cond_t rw_cond;
int rd_cnt, wr_cnt; RWLock(const RWLock&);
RWLock& operator= (const RWLock&); public :
RWLock(): rd_cnt(),wr_cnt()
{
pthread_mutex_init(&cnt_mutex, NULL);
pthread_cond_init(&rw_cond, NULL);
} void get_shared_lock()
{
pthread_mutex_lock(&cnt_mutex);
while (wr_cnt >)
{
pthread_cond_wait(&rw_cond,&cnt_mutex);
}
rd_cnt++;
pthread_mutex_unlock(&cnt_mutex);
} void release_shared_lock()
{
pthread_mutex_lock(&cnt_mutex);
rd_cnt--;
if ( == rd_cnt)
{
pthread_cond_signal(&rw_cond);
}
pthread_mutex_unlock(&cnt_mutex);
} void get_exclusive_lock()
{
pthread_mutex_lock(&cnt_mutex);
while (rd_cnt+wr_cnt>)
{
pthread_cond_wait(&rw_cond,&cnt_mutex);
}
wr_cnt++;
pthread_mutex_unlock(&cnt_mutex);
} void release_exclusive_lock()
{
pthread_mutex_lock(&cnt_mutex);
wr_cnt--;
pthread_cond_broadcast(&rw_cond);
pthread_mutex_unlock(&cnt_mutex);
} ~RWLock()
{
pthread_mutex_destroy(&cnt_mutex);
pthread_cond_destroy(&rw_cond);
}
}; class Test
{ private :
RWLock lock; static void* shared_task_handler(void* arg)
{
Test* testptr = static_cast<Test*>(arg); testptr->lock.get_shared_lock();//得到共享锁
//do the shared task here
testptr->lock.release_shared_lock();
} static void * exclusive_task_handler(void * arg)
{
Test* testptr = static_cast<Test*>(arg);
testptr->lock.get_exclusive_lock();
//do the exclusive task here
testptr->lock.release_exclusive_lock();
} public :
typedef void* (*ThreadFunc) (void*); void start()
{
srand(time(NULL)); const int THREADS_NO=rand()%;
pthread_t* threads = new pthread_t[THREADS_NO]; for(int i=; i<THREADS_NO; i++)
{
ThreadFunc tmpfunc = rand()%? shared_task_handler : exclusive_task_handler;
if (pthread_create(threads+i,NULL,tmpfunc,this))
{
cerr << "pthread_create fails" << endl;
exit();
}
} for(int i=; i<THREADS_NO; i++)
{
pthread_join(threads[i],NULL);
} delete[] threads;
}
}; int main()
{
Test tmptest;
tmptest.start();
}
static_cast < type-id > ( expression )
int i;
float f = 166.71;
i = static_cast<int>(f);
此时结果,i的值为166。
cstdlib是C++里面的一个常用函数库, 等价于C中的<stdlib.h>。
http://blog.chinaunix.net/uid-52437-id-2108727.html

用c++封装linux系统调用的更多相关文章

  1. 别出心裁的Linux系统调用学习法

    别出心裁的Linux系统调用学习法 操作系统与系统调用 操作系统(Operating System,简称OS)是计算机中最重要的系统软件,是这样的一组系统程序的集成:这些系统程序在用户对计算机的使用中 ...

  2. 关于Linux系统调用,内核函数【转】

    转自:http://blog.csdn.net/ubuntulover/article/details/5988220 早上听人说到某个程序的一部分是内核态,另一部分是用户态,需要怎么怎么.当时突然想 ...

  3. Linux系统调用(转载)

    目录: 1. Linux系统调用原理 2. 系统调用的实现 3. Linux系统调用分类及列表 4.系统调用.用户编程接口(API).系统命令和内核函数的关系 5. Linux系统调用实例 6. Li ...

  4. linux内核剖析(六)Linux系统调用详解(实现机制分析)

    本文介绍了系统调用的一些实现细节.首先分析了系统调用的意义,它们与库函数和应用程序接口(API)有怎样的关系.然后,我们考察了Linux内核如何实现系统调用,以及执行系统调用的连锁反应:陷入内核,传递 ...

  5. Linux系统调用、新增系统调用方法【转】

    转自:http://blog.chinaunix.net/uid-25374603-id-3401045.html 说明: 系统调用是内核和应用程序间的接口,应用程序要访问硬件设备和其他操作系统资源, ...

  6. 理解Linux系统调用

    目录 1.什么是系统调用 2.linux的系统调用 3.linux系统调用实现 1.什么是系统调用 系统调用,指的是操作系统提供给用户程序调用的一组特殊接口,用户程序可以根据这组接口获得操作系统内核的 ...

  7. ARM Linux系统调用的原理

    转载自:http://blog.csdn.net/hongjiujing/article/details/6831192 ARM Linux系统调用的原理 操作系统为在用户态运行的进程与硬件设备进行交 ...

  8. Linux系统调用及其效率

    操作系统相关概念: 操作系统---管理计算机硬件与软件资源的软件,是用户与系统操作交互的接口,为在它上面运行的程序提供服务. 操作系统内核 ----操作系统的核心.负责管理系统的进程.内核.设备驱动程 ...

  9. Linux系统调用过程分析

    參考: <Linux内核设计与实现> 0 摘要 linux的系统调用过程: 层次例如以下: 用户程序------>C库(即API):INT 0x80 ----->system_ ...

随机推荐

  1. Drupal、IoT 和开源硬件之间的交集

    导读 来认识一下Amber Matz,她是来自 Lullabot Education 旗下的Drupalize.Me的产品经理以及培训师.当她没有倒腾 Arduino.Raspberry Pi 以及电 ...

  2. 【pyhon】nvshens按目录图片批量下载爬虫1.00(多线程版)

    # nvshens按目录图片批量下载爬虫1.00(多线程版) from bs4 import BeautifulSoup import requests import datetime import ...

  3. 用rpm安装软件的常用步骤

    假设软件叫software.rpm 1.安装前,查看是否安装过用 rpm -q software 2.安装时,用 rpm -ivh software.rpm 3.安装后想删除,用 rpm -e sof ...

  4. 一个异常org.apache.jasper.JasperException: java.lang.IllegalStateException: No output folder:的解决

    今天对一个WebApp做完修改,导出成war包,再发布到Tomcat7中,居然访问不了了! 同样的问题一周前也出现过,后来一顿鼓捣,又莫名其妙好了,当时认为是Tomcat7闹点小毛病,也没多想. 但是 ...

  5. MESI-CPU缓存一致性协议

    转http://blog.csdn.net/realxie/article/details/7317630 http://en.wikipedia.org/wiki/MESI_protocol MES ...

  6. 从主机系统向虚拟机系统里面copy 文件

    从主机系统向虚拟机系统里面copy 文件: 一:请确保你的虚拟机里面安装了 VMTools 1:安装VMTools 2: 进入虚拟机系统里面,如果没有自动运行 VMtool安装程序,请打开我的电脑,手 ...

  7. GDB高级使用方法

    1.设置环境变量 用户可以在GDB的调试环境中定义自己需要的变量,用来保存一些调试程序中的运行数据.要定义一个GDB的变量很简单,只需使用GDB的set命令. GDB的环境变量和Linux一样,也是以 ...

  8. mysql表utf-8 字符串入库编码异常

    分析:http://www.myexception.cn/mysql/639943.html 解决方法:http://blog.sina.com.cn/s/blog_3f78232201011o26. ...

  9. Oracle Unicode转中文(解码)

      Oracle Unicode转中文(解码) CreateTime--2018年3月29日15:23:30 Author:Marydon 情景描述: 将数据库中的某个字段误存储的是Unicode编码 ...

  10. QueryRunner 结果处理器

    package cn.itcast.dbutil; import java.sql.SQLException; import java.util.List; import java.util.Map; ...