当前位置:网站首页>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》
边栏推荐
- How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
- leetcode 474. Ones and Zeroes 一和零(中等)
- Member management applet actual development 07 page Jump
- 2022-2028 global weight loss ginger tea industry research and trend analysis report
- Ranger plug-in development (Part 2)
- Which is better, server rental or hosting services in the United States?
- Quick start of wechat applet -- project introduction
- Tibetan poem PTA
- 2022-2028 global rampant travel industry research and trend analysis report
- CentOS install MySQL
猜你喜欢

C#生成putty格式的ppk文件(支持passphrase)

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

Mindjet mindmanager2022 mind map decompression installer tutorial

C language file operation for conquering C language

Ranger plug-in development (Part 2)

HDU 2488 A Knight's Journey(DFS)

P4学习——Basic Tunneling

Random ball size, random motion collision

20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br

Oracle-数据完整性
随机推荐
2022-2028 global capsule shell industry research and trend analysis report
C#生成putty格式的ppk文件(支持passphrase)
leetcode 474. Ones and Zeroes 一和零(中等)
Vulnerability discovery - App application vulnerability probe type utilization and repair
C # Generate PPK files in Putty format (passthrough support)
Search rotation sort array
Cmu15445 (fall 2019) project 1 - buffer pool details
第53章 从业务逻辑实现角度整体性理解程序
MySQL index test
写给 5000 粉丝的一封信!
[PHP] self developed framework qphp, used by qphp framework
Unit test concept and purpose
C # generates PPK files in putty format (supports passphrase)
Cloud security daily 220630: the IBM data protection platform has found an arbitrary code execution vulnerability, which needs to be upgraded as soon as possible
Wechat official account development (1) introduction to wechat official account
Host FL Studio fruit music production daw20.9
2022-2028 global weight loss ginger tea industry research and trend analysis report
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
剑指 Offer 19. 正则表达式匹配
Length of the longest integrable subarray