当前位置:网站首页>CString在多线程中的问题
CString在多线程中的问题
2022-07-03 15:20:00 【宇龍_】
闲来没事,聊聊CString在多线程中很容易踩的坑,这个坑很可能存在于你现在的项目中。
举例:
int main()
{
CString str = L"test";
CString strThread1 = str;
CString strThread2 = str;
//将两个参数传递到各自的线程中,以下使用伪代码
CThread thread1(strThread1);
CThread thread2(strThread2);
thread1.wait();
thread2.wait();
return 0;
}不要以为strThread1和strThread2只是个临时变量,以值传递的方式传递到线程中就很安全,CString的内部较为复杂,有诸如引用计数,写时复制这样的功能,也就是说,在这段代码中str、strThread1以及strThread2是指向同一段内存的,在多线程中操作一段内存的结果可想而知,呵呵!
在调试代码时可以证明这三个变量指向的是同一段内存:
边栏推荐
- [set theory] inclusion exclusion principle (complex example)
- Jvm-04-runtime data area heap, method area
- Visual upper system design and development (Halcon WinForm) -1 Process node design
- Jvm-09 byte code introduction
- [Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
- Idea does not specify an output path for the module
- 秒杀系统1-登录功能
- 【日常训练】395. 至少有 K 个重复字符的最长子串
- Puppet automatic operation and maintenance troubleshooting cases
- Search in the two-dimensional array of leetcode sword offer (10)
猜你喜欢

视觉上位系统设计开发(halcon-winform)-1.流程节点设计

Redis lock Optimization Practice issued by gaobingfa

Chapter 04_ Logical architecture

Jvm-02-class loading subsystem

Influxdb2 sources add data sources

Popular understanding of ovo and ovr

Characteristics of MySQL InnoDB storage engine -- Analysis of row lock

High quality workplace human beings must use software to recommend, and you certainly don't know the last one

秒杀系统3-商品列表和商品详情

基础SQL教程
随机推荐
Jvm-02-class loading subsystem
视觉上位系统设计开发(halcon-winform)-2.全局变量设计
Kubernetes will show you from beginning to end
Construction of operation and maintenance system
[cloud native training camp] module VIII kubernetes life cycle management and service discovery
Using notepad++ to build an arbitrary language development environment
The method of parameter estimation of user-defined function in MATLAB
整形和浮点型是如何在内存中的存储
求字符串函数和长度不受限制的字符串函数的详解
Kubernetes帶你從頭到尾捋一遍
在MapReduce中利用MultipleOutputs输出多个文件
Enable multi-threaded download of chrome and edge browsers
Final review points of human-computer interaction
视觉上位系统设计开发(halcon-winform)-6.节点与宫格
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
Detailed comments on MapReduce instance code on the official website
Summary of concurrent full knowledge points
Use of Tex editor
什么是one-hot encoding?Pytorch中,将label变成one hot编码的两种方式
【可能是全中文网最全】pushgateway入门笔记