日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: www.av黄色| 男男成人高潮片免费网站 | 天天综合网站 | 日日夜夜狠狠 | 91手机在线观看 | 欧美a视频| 亚洲综合在线一区二区 | a级全黄 | 婷婷开心激情网 | 黄色三级三级三级三级 | 亚洲无吗在线 | 国产一级做a爰片久久毛片男 | 亚洲精品中文字幕在线观看 | 婷婷综合一区 | 张柏芝亚洲一区二区三区 | 国产日韩成人内射视频 | 91精品国产闺蜜国产在线闺蜜 | 国产91片 | 偷拍一区二区三区 | 精品久久人人妻人人做人人 | 麻豆视频91 | 日韩电影在线观看一区二区 | 国产素人在线观看 | 成人免费黄色大片v266 | 懂色av一区二区三区免费观看 | 国产一区两区 | 日本网站免费观看 | 日韩av区 | 成人在线电影网站 | 日韩天堂网| 你懂的网站在线观看 | 久久久久成人精品免费播放动漫 | 情欲超 | 少妇做爰免费视频播放 | 色老大影院 | 亚色影库 | 免费观看在线观看 | 男女性生活视频网站 | 成人在线三级 | 日本一级淫片色费放 | www国产亚洲精品久久麻豆 | 射影院 | 久久中文字幕视频 | 69xx免费视频| 夜夜cao | 亚洲第一区在线播放 | 国产成人精品免费在线观看 | 女人脱了内裤趴开腿让男躁 | 亚洲综合套图 | 992在线观看 | 91九色丨porny丨肉丝 | 日韩怡春院 | 国产成人在线影院 | 国产精品久久久久久精 | 四虎影音先锋 | 免费看成年人视频 | 90岁老太婆乱淫 | 天天曰天天爽 | 欧美第一夜 | 色老汉av一区二区三区 | 亚洲国产精品久久久久久久 | 久久色图| 韩国在线不卡 | 伊人久综合 | 97潮色 | 天天操狠狠干 | 亚洲五月婷 | 不卡一二区 | 久久免费播放 | 尤物精品视频在线观看 | 亚洲免费色 | 超碰在线观看99 | 久操国产视频 | 白浆av | 亚洲影视一区 | 哺乳援交吃奶在线播放 | a午夜 | 日韩色吧 | 国产自偷| 日韩少妇内射免费播放18禁裸乳 | 欧美黑人又粗又大又爽免费 | 999午夜 | 无码国产精品一区二区色情男同 | 亚洲福利网 | 暖暖av在线 | 国产精品一卡二卡在线观看 | 亚洲女人被黑人巨大进入 | 永久免费看片在线播放 | 亚洲一区二区三区四 | 色狠狠一区二区三区香蕉 | 日韩av成人网 | 欧美一性一乱一交一视频 | 亚洲free性xxxx护士白浆 | 丝袜 中出 制服 人妻 美腿 | 天天干天天要 | 激情亚洲色图 | 水多多在线| 自慰无码一区二区三区 | 日韩成人精品视频 |