当前位置:网站首页>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是指向同一段内存的,在多线程中操作一段内存的结果可想而知,呵呵!
在调试代码时可以证明这三个变量指向的是同一段内存:
边栏推荐
- Redis主从、哨兵、集群模式介绍
- 驱动与应用程序通信
- [cloud native training camp] module VIII kubernetes life cycle management and service discovery
- Jvm-03-runtime data area PC, stack, local method stack
- [combinatorics] combinatorial identities (recursive combinatorial identities | sum of variable terms | simple combinatorial identities and | sum of variable terms | staggered sums of combinatorial ide
- XWiki安装使用技巧
- [cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
- 开启 Chrome 和 Edge 浏览器多线程下载
- 使用JMeter对WebService进行压力测试
- Jvm-04-runtime data area heap, method area
猜你喜欢

Summary of JVM knowledge points

What is machine reading comprehension? What are the applications? Finally someone made it clear

秒杀系统1-登录功能

Final review points of human-computer interaction

What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry

【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现

Kubernetes will show you from beginning to end

Seckill system 3- product list and product details

高并发下之redis锁优化实战

视觉上位系统设计开发(halcon-winform)-1.流程节点设计
随机推荐
视觉上位系统设计开发(halcon-winform)
Popular understanding of linear regression (II)
Kubernetes帶你從頭到尾捋一遍
视觉上位系统设计开发(halcon-winform)-2.全局变量设计
秒杀系统1-登录功能
Redis cache penetration, cache breakdown, cache avalanche solution
Apache ant extension tutorial
使用Tengine解决负载均衡的Session问题
Jvm-06-execution engine
Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
Jvm-08-garbage collector
Visual upper system design and development (Halcon WinForm) -1 Process node design
Global and Chinese markets for indoor HDTV antennas 2022-2028: Research Report on technology, participants, trends, market size and share
Popular understanding of decision tree ID3
视觉上位系统设计开发(halcon-winform)-1.流程节点设计
SQL server安装位置改不了
视觉上位系统设计开发(halcon-winform)-5.相机
详解指针进阶1
Relationship between truncated random distribution and original distribution
秒殺系統3-商品列錶和商品詳情