当前位置:网站首页>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是指向同一段内存的,在多线程中操作一段内存的结果可想而知,呵呵!
在调试代码时可以证明这三个变量指向的是同一段内存:
边栏推荐
- PyTorch crop images differentiablly
- Using Tengine to solve the session problem of load balancing
- 从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
- Seckill system 3- product list and product details
- GCC cannot find the library file after specifying the link library path
- Summary of concurrent full knowledge points
- QT common sentence notes
- Kubernetes - YAML文件解读
- Visual upper system design and development (Halcon WinForm) -6 Nodes and grids
- Kubernetes - yaml file interpretation
猜你喜欢
【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
基础SQL教程
Popular understanding of ovo and ovr
Introduction, use and principle of synchronized
What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding
如何使用 @NotNull等注解校验 并全局异常处理
运维体系的构建
Basic SQL tutorial
北京共有产权房出租新规实施的租赁案例
[probably the most complete in Chinese] pushgateway entry notes
随机推荐
Jvm-09 byte code introduction
需要知道的字符串函数
Construction of operation and maintenance system
【可能是全中文网最全】pushgateway入门笔记
Unity hierarchical bounding box AABB tree
Matplotlib drawing label cannot display Chinese problems
互斥对象与临界区的区别
[combinatorial mathematics] binomial theorem and combinatorial identity (binomial theorem | three combinatorial identities | recursive formula 1 | recursive formula 2 | recursive formula 3 Pascal / Ya
C语言刷题~Leetcode与牛客网简单题
软件安装信息、系统服务在注册表中的位置
Visual upper system design and development (Halcon WinForm) -1 Process node design
自定义注解
Markdown file titles are all reduced by one level
[probably the most complete in Chinese] pushgateway entry notes
socket. IO build distributed web push server
Stress test WebService with JMeter
Relationship between truncated random distribution and original distribution
Visual upper system design and development (Halcon WinForm) -4 Communication management
【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
Redis lock Optimization Practice issued by gaobingfa