• 用c++封装linux系统调用


    #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(0),wr_cnt(0)
            {
                pthread_mutex_init(&cnt_mutex, NULL);
                pthread_cond_init(&rw_cond, NULL);
            }
    
        void get_shared_lock()
            {
                pthread_mutex_lock(&cnt_mutex);
                while (wr_cnt >0)
                    {
                        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 (0 == 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>0)
                    {
                        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()%100;
                pthread_t* threads = new pthread_t[THREADS_NO];
    
                for(int i=0; i<THREADS_NO; i++)
                    {
                        ThreadFunc tmpfunc = rand()%2? shared_task_handler : exclusive_task_handler;
                        if (pthread_create(threads+i,NULL,tmpfunc,this))
                            {
                                cerr << "pthread_create fails" << endl;
                                exit(1);
                            }
                    }
    
                for(int i=0; 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
  • 相关阅读:
    Android基础笔记(十八)- Fragment
    fedora20配置静态ip
    读《编程之美》励志篇
    官方教程Stealth学习笔记(一)
    从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
    POJ2186 Popular Cows [强连通分量|缩点]
    HDU2767Proving Equivalences[强连通分量 缩点]
    POJ1236Network of Schools[强连通分量|缩点]
    [USACO14OPEN] Dueling GPS's[最短路建模]
    洛谷2448 无尽的生命[树状数组 离散化]
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4151019.html
Copyright © 2020-2023  润新知