当前位置:网站首页>Problem solving: how to manage thread_local pointer variables
Problem solving: how to manage thread_local pointer variables
2022-07-01 00:43:00 【51CTO】
introduction
The problem arises when implementing a function of the project , I need a thread_local The pointer to , Because the whole project has almost been realized , But in the end, I found the problem of memory leakage . Because it implements a library that is biased towards the bottom , And the use of memory is basically under control , So at the beginning of the project implementation, smart pointers were not used for efficiency , But later, there was a memory leak problem here .
The original code is almost like this :
In this way, it is difficult to release the thread after it ends , So we need to think of a way to do this , And don't want to change the original code on a large scale , The final solution is :
Don't worry about variable names , The basic idea is to use smart pointers to manage memory , Let's look at a simulated small 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
<<
" End of the function \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.
Output :
In this way, the expectation is achieved .
In the process of solving the problem, I also found boost There is also a gadget in the library that can do this , namely boost::thread_specific_ptr, Its function is to store per thread , And it will execute the preset callback when the thread ends , It is similar to a special smart pointer , Let's see how it works :
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
<<
" End of the function \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.
Execute the following command :
We can put thread_specific_ptr Use as a normal pointer , Because there are not many member functions , Compared with the smart pointer, it can be said that one sky and one earth . Remove the output for the pointer value , We can see that other outputs are the same :
Yes thread_specific_ptr Interested friends can go to see their documents , Link at the end of the article .
Reference resources :
- file 《 boost::thread_specific_ptr》
边栏推荐
- 2022-2028 global elevator emergency communication system industry research and trend analysis report
- Multi graph explanation of resource preemption in yarn capacity scheduling
- 2022-2028 global carbon fiber room scraper system industry research and trend analysis report
- When is it appropriate to replace a virtual machine with a virtual machine?
- 双链表:初始化 插入 删除 遍历
- HDU 2488 A Knight's Journey(DFS)
- 20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
- Integer to hexadecimal string PTA
- 2022-2028 global electric yacht industry research and trend analysis report
- The programmer's girlfriend gave me a fatigue driving test
猜你喜欢

2022-2028 global ICT test probe industry research and trend analysis report

剑指 Offer 19. 正则表达式匹配

Deployment of mini version message queue based on redis6.0

CentOS install MySQL

CTF tool (1) -- archpr -- including installation / use process

20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking

MySQL variables, stored procedures and functions

C # generates PPK files in putty format (supports passphrase)

Vulnerability discovery - App application vulnerability probe type utilization and repair

Redis - cache penetration, cache breakdown, cache avalanche
随机推荐
[DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
Can JDBC based on openjdk connect to MySQL?
Teach you how to use Hal library to get started -- become a lighting master
[untitled]
Oracle临时表详解
Design e-commerce seckill system
Random ball size, random motion collision
Two-stage RO: part 1
The programmer's girlfriend gave me a fatigue driving test
What SQL statements are supported for data filtering
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
Bugku CTF daily one question dark cloud invitation code
Multi graph explanation of resource preemption in yarn capacity scheduling
Oracle-表的创建与管理
Experiment 8 T-SQL, stored procedure
实验八 T-sql,存储过程
P4学习——p4runtime
Which is better, server rental or hosting services in the United States?
NE555波形发生器手把手教程之NE555内部结构(一)