日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

thread local storage

發布時間:2024/9/30 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 thread local storage 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有時會需要這種模式,一個全局變量,需要在程序的任何地方都可以使用它,但是當這個變量出現在不同線程時,就要求系統將這個變量拷貝到各個線程中,這樣的話,每個線程內部也可以隨時訪問本線程的全局變量,但是線程之間的這個變量沒有任何關系。這樣就避免了鎖,會提高程序運行的效率。在java中是ThreadLocal, 在boost中是

thread_specific_ptr。

在linux下C的方法是?_thread


//a.h #ifndef __A_H #define __A_H #include <string>using namespace std; class A {public:string s;int i; };#endif//c.h#ifndef __C_H #define __C_H //#include <boost/thread/tss.hpp> #include "a.h" #include <pthread.h> #include <unistd.h> //using namespace boost; class C {public://static thread_specific_ptr<A> pool;static __thread A *pool;static void init() {pool = new A();}/*static __thread A *instance() {pool = new A();return pool;} */ };__thread A *C::pool; #endif//list.h #ifndef __LIST_H #define __LIST_H #include "c.h" class list {public:int start;int step;list(int s,int t):start(s), step(t){C::init();C::pool->i = s;}void inc() {C::pool->i += step;}int get() {return C::pool->i;} };#endif//main.cpp#include "c.h" #include "list.h" #include <iostream> #include <pthread.h> #include <unistd.h>using namespace std;int i = 1; int s = 1; void *run(void *junk) {list ls(i, s);for (int ind = 0; ind < 10; ++ind) {cout << (size_t)pthread_self() << ": "<< ls.get()<<endl;ls.inc();sleep(2);// cout << (size_t)pthread_self() << ": "<< ls.get()<<endl;}}int main() {pthread_t t_a, t_b;pthread_create(&t_a, NULL, run, (void *)NULL);sleep(2);i += 1000;s = 5;pthread_create(&t_b, NULL, run, (void*)NULL);pthread_join(t_a, NULL);pthread_join(t_b, NULL);return 0; }


g++ -lpthread ?main.cpp -o test


output:

1085499712: 1
1095989568: 1001
1085499712: 2
1095989568: 1006
1085499712: 3
1095989568: 1011
1085499712: 4
1095989568: 1016
1085499712: 5
1095989568: 1021
1085499712: 6
1095989568: 1026
1085499712: 7
1095989568: 1031
1085499712: 8
1095989568: 1036
1085499712: 9
1095989568: 1041
1085499712: 10
1095989568: 1046


如果將c.h中的__thread都注釋掉的,數據就不一致了:

1110722880: 1
1110722880: 2
1121212736: 1001
1121212736: 1006
1110722880: 1011
1121212736: 1012
1110722880: 1017
1121212736: 1018
1110722880: 1023
1121212736: 1024
1110722880: 1029
1121212736: 1030
1110722880: 1035
1121212736: 1036
1110722880: 1041
1121212736: 1042
1110722880: 1047
1121212736: 1048
1110722880: 1053
1121212736: 1054



如果用boost,更簡單


//c.h #ifndef __C_H #define __C_H #include <boost/thread/tss.hpp> #include "a.h" #include <pthread.h> #include <unistd.h> using namespace boost; class C {public:static thread_specific_ptr<A> pool;//static __thread A *pool;//static void init() {// pool = new A();// }/*static __thread A *instance() {pool = new A();return pool;} */ };thread_specific_ptr<A> C::pool; //__thread A *C::pool; #endif// list.h #ifndef __LIST_H #define __LIST_H #include "c.h" class list {public:int start;int step;list(int s,int t):start(s), step(t){//C::init();C::pool.reset(new A);C::pool->i = s;}void inc() {C::pool->i += step;}int get() {return C::pool->i;} };#endif
g++ main.cpp -I~/boost -lboost_thread -lpthread -o test



1086380352: 1
1117477184: 1001
1086380352: 2
1117477184: 1006
1086380352: 3
1117477184: 1011
1086380352: 4
1117477184: 1016
1086380352: 5
1117477184: 1021
1086380352: 6
1117477184: 1026
1086380352: 7
1117477184: 1031
1086380352: 8
1117477184: 1036
1086380352: 9
1117477184: 1041
1086380352: 10
1117477184: 1046



http://www.searchtb.com/2012/09/tls.html

此博客也介紹了線程局部變量的對比,至于__thread容易造成內存泄露,不是很明白


總結

以上是生活随笔為你收集整理的thread local storage的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。