zoukankan      html  css  js  c++  java
  • c++11特性之std:call_once

    使用背景
    在多线程编程环境中,我们有时希望某个操作只做一次,比如获取一些系统变量;或者某个函数只被调用一次。我们就可以采用系统提供的std::call_once来保证。

    我们再看下call_once的定义:
    template< class Callable, class... Args >
    void call_once( std::once_flag& flag, Callable&& f, Args&&... args );
    用法示例
    // 将文件保存为 call.cpp
    // 编译:g++ -o test call.cpp -std=c++11 -pthread
    // 运行:./test
    #include <iostream>
    #include <thread>
    #include <mutex>
    #include <sys/time.h>
    #include <sys/resource.h>
    
    int GetMaxOpenFileSys(struct rlimit *rlim)
    {
    	// 获取最大文件描述符数量
    	::getrlimit(RLIMIT_NOFILE, rlim);
    	
    	printf("GetMaxOpenFileSys Called
    ");
    }
    
    int GetMaxOpenFile()
    {
    	static std::once_flag once_flag;
    	static struct rlimit  rlimit;
    
    	// 保证在多线程环境下,只调用GetMaxOpenFileSys一次
    	// 也就是说保证只调用::getrlimit一次
        std::call_once(once_flag, GetMaxOpenFileSys, &rlimit);
    
    	return rlimit.rlim_max;
    }
    
    void ThreadEntry()
    {
    	// 线程入口函数,我们先获取系统允许可打开的最多文件描述符数量
    	printf("MaxOpenFile:%d
    ", GetMaxOpenFile());
    }
    
    int main(int argc, char ** argv)
    {
        std::thread xThreadA(ThreadEntry);
        std::thread xThreadB(ThreadEntry);
        std::thread xThreadC(ThreadEntry);
        std::thread xThreadD(ThreadEntry);
    
    	xThreadA.join();
        xThreadB.join();
        xThreadC.join();
        xThreadD.join();
    
        return 0;
    }
    
    程序运行结果:
    motadou@dev-0-0:/home/motadou/objs/test$ ./test 
    GetMaxOpenFileSys Called
    MaxOpenFile:4096
    MaxOpenFile:4096
    MaxOpenFile:4096
    MaxOpenFile:4096
    
    
    从上面的运行结果,我们就可以看到程序在多线程环境中,只调用了GetMaxOpenFileSys()一次。

  • 相关阅读:
    Linux 服务器配置网站以及绑定域名
    vmware 下的三种网络模式
    排序算法-python版
    python实例2-写一个爬虫下载小功能
    python学习-1-列表,元组,字典,集合,文件操作
    linux 10 -Bash Shell编程
    [ActionScript 3.0] AS3 3D双圆环贴图
    [ActionScript 3.0] AS3 3D星形贴图
    [ActionScript 3.0] AS3 绘制星形
    [ActionScript 3.0] AS3 获取函数参数个数
  • 原文地址:https://www.cnblogs.com/motadou/p/4370279.html
Copyright © 2011-2022 走看看