Static in the c++11

Source: Internet
Author: User

  • Awesome thread-safe initialization

    Static pLSA::P lsamodel& model ()

    {

    Static pLSA::P Lsamodel _model = ([&] () {

    pLSA::P Lsamodel _model;

    String Modelpath = "./data/plsa.model/pzw.idx";

    Psconf (Modelpath, "plsatopic");

    _model. Load (Modelpath);

    return _model;

    })();

    return _model;

    }

    ??

    This code is thread-safe, and c++11 guarantees that initialization constructs or assignment functions are only called once!

    ??

  • Better Thread Local

    The global variable inside the thread makes the code more concise, for example, I have a word breaker, and the dictionary data is globally unique and can be read

    Static variables, a buffer is required to split the data inside each thread, so that thread-safe threads do not need to open this space, the speed is optimal, then this buffer can be used with thread local

    static bool Init (int seg_buff_size = seghandle::seg_buff_size, String data_dir = "./data/wordseg", int type = Seg_use_defa ULT, String conf_path = "./conf/scw.conf")

    {

    Only once init static dictionary data static initialization

    static BOOL ret = init (DATA_DIR.C_STR (), type, conf_path.c_str ());

    ??

    if (!ret)

    {

    return false;

    }

    ??

    Only once init for each thread participle internal thread internal buffer data thread Local

    static thread_local bool ishandleinited = false;

    if (!ishandleinited)

    {

    Handle (). Init (seg_buff_size);

    Ishandleinited = true;

    }

    ??

    return ret;

    }

    ??

    Static seghandle& handle ()

    {

    static thread_local Seghandle _handle;

    return _handle;

    }

    ??

Static in the c++11

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.