当前位置:网站首页>问题解决:如何管理线程私有(thread_local)的指针变量
问题解决:如何管理线程私有(thread_local)的指针变量
2022-07-01 00:35:00 【51CTO】
引言
问题出现在实现项目的一个功能的时候,我需要一个thread_local的指针,因为整个项目已经差不多实现了,但在最后发现了内存泄露的问题。因为实现的是一个偏向于底层的库,且内存的使用基本都在掌握之内,所以在项目实现之初为了效率并没有使用智能指针,但在后来在这里出现了内存泄露问题。
原本代码差不多是这样:
这样在线程结束以后很难去把它释放掉,所以需要想一个方法做到这样,且不想大规模改动原有代码,最终的解决方案是这样的:
不必在意变量名,基本思路还是使用智能指针去管理内存,我们来看一个模拟的小demo:
using
namespace
std;
void
Delete_(
int
*
para){
if(
para
==
nullptr)
return;
cout
<<
"nihao\n";
delete
para;
return;
}
thread_local
int
*
env
=
nullptr;
thread_local
std::unique_ptr
<
int,
decltype(
Delete_)
*>
Env(
env,
Delete_);
void
*
Routine(
void
*
para){
Env.
reset();
env
=
new
int(
5);
Env.
reset(
env);
cout
<<
"函数结束\n";
return
0;
}
int
main(){
pthread_t
tid[
5];
for(
size_t
i
=
0;
i
<
2;
i
++){
pthread_create(
tid
+
i,
nullptr,
Routine,
0);
}
for(
size_t
i
=
0;
i
<
2;
i
++){
pthread_join(
tid[
i],
nullptr);
}
sleep(
1);
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
输出:
这样就达到了预期。
在解决问题的过程中还发现了boost库中也有一个玩意儿可以做到这样,即boost::thread_specific_ptr,它的作用就是用于每线程存储,且会在线程结束的时候执行预先设置的回调,就类似于一个特殊的智能指针,我们来看看其如何应用:
using
namespace
std;
void
Delete_(
int
*
para){
if(
para
==
nullptr)
return;
cout
<<
"nihao\n";
delete
para;
return;
}
boost::thread_specific_ptr
<
int
>
env {
&
Delete_};
void
*
Routine(
void
*
para){
if(
!
env.
get()){
env.
reset(
new
int(
5));
}
cout
<<
*
env
<<
endl;
cout
<<
"函数结束\n";
return
0;
}
int
main(){
pthread_t
tid[
5];
for(
size_t
i
=
0;
i
<
2;
i
++){
pthread_create(
tid
+
i,
nullptr,
Routine,
0);
}
for(
size_t
i
=
0;
i
<
2;
i
++){
pthread_join(
tid[
i],
nullptr);
}
sleep(
1);
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
执行以下命令:
我们可以在使用时把thread_specific_ptr当做普通指针来用,因为其本身的成员函数并不多,相比与智能指针来说可谓一个天上一个地下。除去对于指针值的输出,我们可以看到其他输出都是一样的:
对thread_specific_ptr有兴趣的朋友可以去看看其文档,链接在文末。
参考:
- 文档《 boost::thread_specific_ptr》
边栏推荐
- Wordpress blog uses volcano engine veimagex for static resource CDN acceleration (free)
- Summer Challenge [FFH] harmonyos mobile phone remote control Dayu development board camera
- Random ball size, random motion collision
- 2022-2028 global rotary transmission system industry research and trend analysis report
- 双链表:初始化 插入 删除 遍历
- Basic knowledge of Embedded Network - introduction of mqtt
- 2022-2028 global single travel industry research and trend analysis report
- Multi graph explanation of resource preemption in yarn capacity scheduling
- 2022-2028 global rampant travel industry research and trend analysis report
- ABAQUS 2022 latest edition - perfect realistic simulation solution
猜你喜欢

Member management applet actual development 07 page Jump

Multi graph explanation of resource preemption in yarn capacity scheduling
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

Gateway service gateway

2022-2028 global public address fire alarm system industry research and trend analysis report

2022-2028 global retro glass industry research and trend analysis report

2022-2028 global electric yacht industry research and trend analysis report

2022-2028 global PTFE lined valve industry research and trend analysis report

Introduction to ES6 promise, new features of ES7 and es8 async and await

Design e-commerce seckill system
随机推荐
Redis - how to understand publishing and subscribing
Teach you how to use Hal library to get started -- become a lighting master
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
Pytorch auto derivation
1009 product of polynomials (25 points) [PTA class A]
Kubernetes ---- pod configuration container start command
"Experience" my understanding of user growth "new users"
2022-2028 global PTFE lined valve industry research and trend analysis report
Ranger plug-in development (Part 2)
2022就要过去一半了,挣钱好难
Redis - understand the master-slave replication mechanism
A detailed explanation of the implementation principle of go Distributed Link Tracking
The difference between union and union all in MySQL
ABAQUS 2022 latest edition - perfect realistic simulation solution
给按钮的边框和文字设置不同的背景色
解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
SSM integration process (integration configuration, function module development, interface test)
2022-2028 global retro glass industry research and trend analysis report
Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
CentOS install MySQL