当前位置:网站首页>问题解决:如何管理线程私有(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》
边栏推荐
- Gateway service gateway
- 深度学习的历史
- Makefile notes (Yiwen Institute makefile)
- Member management applet actual development 07 page Jump
- Redis - understand the master-slave replication mechanism
- The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team
- Rust book materials - yazhijia Library
- 2022-2028 global mobile scanning radiology room industry survey and trend analysis report
- MySQL storage engine
- 2022-2028 global 3D printing ASA consumables industry research and trend analysis report
猜你喜欢

Oracle-表的创建与管理

20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute

Host FL Studio fruit music production daw20.9

Deployment of mini version message queue based on redis6.0

剑指 Offer 18. 删除链表的节点

Teach you how to use Hal library to get started -- become a lighting master

2022-2028 global herbal diet tea industry research and trend analysis report

2022-2028 global rotary transmission system industry research and trend analysis report

Vulnerability discovery - App application vulnerability probe type utilization and repair

Confirm() method of window
随机推荐
CentOS installation starts redis
SSM integration process (integration configuration, function module development, interface test)
Redis - how to understand publishing and subscribing
What SQL statements are supported for data filtering
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
在指南针上买基金安全吗?
Excuse me, does Flink support synchronizing data to sqlserver
Oracle临时表详解
优质的水泵 SolidWorks模型素材推荐,不容错过
Line number of Jenkins pipeline script execution exception
How do it outsourcing resident personnel position their pain points?
MySQL storage engine
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
2022-2028 global ICT test probe industry research and trend analysis report
2022-2028 global PTFE lined valve industry research and trend analysis report
【日常记录】——对BigDecimal除法运算时遇到的Bug
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
Basic knowledge of Embedded Network - introduction of mqtt
C#生成putty格式的ppk文件(支持passphrase)