C++11 多线程中的call once

  原文转载于:http://blog.csdn.net/cywosp/article/details/8968655

    在多线程的环境下,有些时候我们不需要某给函数被调用多次或者某些变量被初始化多次,它们仅仅只需要被调用一次或者初始化一次即可。很多时候我们为了初始化某些数据会写出如下代码,这些代码在单线程中是没有任何问题的,但是在多线程中就会出现不可预知的问题。

  1. bool initialized = false; // global flag
  2. if (!initialized) {
  3. // initialize if not initialized yet
  4. initialize ();
  5. initialized = true;
  6. }
  7. or
  8. static std:: vector< std:: string> staticData;
  9. void foo ()
  10. {
  11. if (staticData.empty ()) {
  12. staticData = initializeStaticData ();
  13. }
  14. ...
  15. }

    为了解决上述多线程中出现的资源竞争导致的数据不一致问题,我们大多数的处理方法就是使用互斥锁来处理。在C++11中提供了最新的处理方法:使用std::call_once函数来处理,其定义如下头文件#include<mutex>

  1. template< class Function, class... Args >
  2. void call_once ( std::once_flag& flag, Function&& f, Args&& args... );
  3. 参数解析Parameters:
  4. flag - an object, for which exactly one function gets executed
  5. f - 需要被调用的函数
  6. args... - 传递给函数f的参数(可以多个)
  7. 返回值为 (none)
  8. 抛出异常
  9. std::system_error if any condition prevents calls to call_once from executing as specified any exception thrown by f

例:
  1. static std:: vector< std:: string> staticData;
  2. std:: vector< std:: string>initializeStaticData ()
  3. {
  4. std:: vector< std:: string> vec;
  5. vec.push_back ( "initialize");
  6. return vec;
  7. }
  8. void foo()
  9. {
  10. static std::once_flag oc;
  11. std::call_once(oc, [] { staticData = initializeStaticData ();});
  12. }
正如上面的例子所示call_once函数第一个参数是std::once_flag的一个对象,第二个参数可以是函数、成员函数、函数对象、lambda函数。在实际的使用过程中,我们会经常将该函数用于延后初始化或者类的单例实现(Singleton)。
  1. class X {
  2. private:
  3. mutable std::once_flag initDataFlag;
  4. void initData ()
  5. {
  6. _data = "init";
  7. }
  8. std:: string _data;
  9. public:
  10. std:: string& getData () {
  11. std::call_once (initDataFlag, &X::initData, this);
  12. return _data;
  13. }
  14. };

关于异常Exception: 由被调用的函数产生出的异常都将会重新抛出,因此你最好有异常处理。如果第一次调用没有成功的话,那么第二次还会继续调用,一次类推直到调用成功为止。

关于程序编译:需要加 -std=c++0x或者-std=c++11和-lpthread选项

猜你喜欢

转载自blog.csdn.net/business122/article/details/80883588