Awesome thread-safe initializationStatic 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 LocalThe 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;
}
??