日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

thread local storage

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

有時會需要這種模式,一個全局變量,需要在程序的任何地方都可以使用它,但是當這個變量出現(xiàn)在不同線程時,就要求系統(tǒng)將這個變量拷貝到各個線程中,這樣的話,每個線程內(nèi)部也可以隨時訪問本線程的全局變量,但是線程之間的這個變量沒有任何關系。這樣就避免了鎖,會提高程序運行的效率。在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都注釋掉的,數(shù)據(jù)就不一致了:

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容易造成內(nèi)存泄露,不是很明白


總結

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 天天综合网天天综合 | 91麻豆免费视频 | 熟妇人妻中文av无码 | 亚洲自拍小视频 | 草草影院最新网址 | 国产在线网站 | 黄色香蕉网 | 夜夜爽夜夜叫夜夜高潮漏水 | 黄网www| 97自拍偷拍 | 特级一级黄色片 | 欧美精品一级 | 精品日韩制服无码久久久久久 | 欧美视频不卡 | 国产噜噜噜噜噜久久久久久久久 | 亚洲精品视频大全 | 伊人欧美| 国产a自拍| 琪琪午夜伦理 | 日本在线看 | 欧美老熟妇xb水多毛多 | 日本一二三区在线视频 | 国产99久久九九精品无码免费 | 操亚洲美女 | 97人妻精品一区二区三区免 | 黑森林av| www.日本精品| 奇米色播| 精品无码一区二区三区爱欲 | 欧美无砖砖区免费 | 亚洲a一区| 厕拍极品| 99久久久无码国产精品性 | 人人射人人爱 | 午夜剧场免费看 | www.四虎. | 伊人自拍| aaaa黄色片 | 国产欧美日韩综合精品 | 久久久久久美女 | 免费黄色三级 | 丰满人妻一区二区三区53视频 | 国产人妻人伦精品1国产丝袜 | 五月天av影院 | 亚洲精品免费在线观看 | 好吊日在线| 在线观看波多野结衣 | 久久在线精品视频 | 免费的一级黄色片 | 激情五月婷婷久久 | 欧美一区二区三区免费看 | 4438x亚洲 | 色狠狠一区二区三区 | 青青草原av在线 | 丁香九月激情 | 伊伊总综合网 | 四季av中文字幕一区 | 黄网址在线观看 | 亚洲区小说区图片区qvod | 99精品在线免费视频 | 国产高清在线一区 | 红桃视频网站 | 国产成人精品一区二区三区无码熬 | 日韩性猛交ⅹxxx乱大交 | 国产中文字幕乱人伦在线观看 | 欧美高清x | 超碰人人搞 | 视频在线观看免费大片 | 色视频一区二区 | 胖女人做爰全过程 | 国产99久久久国产精品成人免费 | 久久女女| 十大污网站| 欧美一级黑人 | 久久久精品国产sm调教 | 亚洲欧美综合自拍 | 毛片在线视频播放 | 东北女人啪啪ⅹxx对白 | 亚洲深夜福利 | 91另类| 国产午夜无码精品免费看奶水 | a级片视频网站 | 日韩无码电影 | 亚洲欧美一区二区三区在线 | 欧美一区二区三区观看 | 一级欧美一级日韩 | 免费观看国产精品视频 | 精品欧美一区二区三区在线观看 | 亚洲精品1区 | 成人无高清96免费 | 激情片网站 | 插插操操| 亚洲精品国产一区二区 | 制服丝袜天堂 | 国产无玛 | 午夜亚洲一区 | 黄色aa级片 | 亚洲30p | 天堂av2021|