Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a C++ ThreadLocal

Tags:

c++

c++11

Do anyone know the best way to implement ThreadLocal in C++ whereby we can set and get values passed when neccesary.

I was reading about ThreaLocal in wikipedia and it says;

C++0x introduces the thread_local keyword. Aside that, various C++ compiler implementations provide specific ways to declare thread-local variables:

Do anyone know the gcc declaration for this and perhaps its usage?

like image 296
Bitmap Avatar asked Jan 19 '23 03:01

Bitmap


1 Answers

This is usually a part of whatever the threading library that your OS uses. In Linux, thread local storage is handled with pthread_key_create, pthread_get_specific and pthread_set_specific functions. Most threading libraries will encapsulate this though, and offer a C++ interface. In Boost, it is the thread_specific_ptr...

like image 165
David Rodríguez - dribeas Avatar answered Jan 31 '23 05:01

David Rodríguez - dribeas