当前位置:网站首页>C multithreading learning note 2
C multithreading learning note 2
2022-06-10 13:58:00 【HollowKnightZ】
Use Thread Some do's and don 'ts
Thread visible
AllocateDataSlot、SetData、 GetData
variable var => thread t1,t2
(1) t1,t2 share var => public Yes “ lock ” The problem of
(2) t1,t2 Each has a var => internal There is no lock contention problem
var slot = Thread.AllocateDataSlot("username");
// Set the slot on the main thread , in other words hello word Can only be read by the main thread , Other threads cannot read
Thread.SetData(slot, "hello word!!");
new Thread(() =>
{
Thread.SetData(slot, "hello!!");
var obj = Thread.GetData(slot);
Console.WriteLine(" Sub thread :{0}",obj);
}).Start();
var obj2 = Thread.GetData(slot);
Console.WriteLine(" The main thread :{0}",obj2);
Console.ReadKey();
Output results : Belong to the situation (2)
Performance enhanced version :ThreadStatic
[ThreadStatic]
static string username = "??!!";
static void Main(string[] args)
{
username = "hello word!!";
new Thread(() =>
{
username = "hello!!";
Console.WriteLine(" Sub thread :{0}", username);
}).Start();
Console.WriteLine(" The main thread :{0}", username);
Console.ReadKey();
}
Running results : Belong to the situation (2)
ThreadLocal: Also called thread visibility
ThreadLocal<string> local = new ThreadLocal<string>();
local.Value = "hello word!!";
new Thread(() =>
{
local.Value = "hello!!";
Console.WriteLine(" Sub thread :{0}", local.Value);
}).Start();
Console.WriteLine(" The main thread :{0}", local.Value);
Console.ReadKey();
Running results : Belong to the situation (2)
summary
These data are stored in the thread environment , Is the space overhead of the thread .
Memory fence
release and debug The difference in performance
In actual projects , The release versions are release edition , instead of debug edition , because release Some code and cache optimizations have been done in , For example, some data from memory Read in the CPU In cache . This operation may bring some bug.
The following code is in release There is a problem that the main thread cannot end execution in the environment .
var isStop = false;
Thread t = new Thread(() =>
{
var isSuccess = false;
while (!isStop)
{
isSuccess = !isSuccess;
}
});
t.Start();
Thread.Sleep(1000);
isStop = true;
t.Join();
Console.WriteLine(" End of main thread execution ");
Console.ReadKey();
From the code, it can be found that two threads are sharing one isStop Variable , And threads t This variable will be loaded into CPU Cache in , When the main thread pair isStop After modification , Threads t Can't perceive .
Two solutions :
1. Don't let multiple threads operate on a shared variable , Otherwise, it is easy to have problems .
2. If it has to be done , You do not need to cache , Every time from memory Read data from .
MemoryBarrier and VolatileRead
All memory writes prior to this method should be made from CPU Cache Update to memory, All memory reads after this method are from memory Read in instead of CPU Cache.
MemoryBarrier:
var isStop = false;
Thread t = new Thread(() =>
{
var isSuccess = false;
while (!isStop)
{
Thread.MemoryBarrier();
isSuccess = !isSuccess;
}
});
t.Start();
Thread.Sleep(1000);
isStop = true;
t.Join();
Console.WriteLine(" End of main thread execution ");
Console.ReadKey();
VolatileRead:
var isStop = 0;
Thread t = new Thread(() =>
{
var isSuccess = false;
while (isStop != 1)
{
Thread.VolatileRead(ref isStop);
isSuccess = !isSuccess;
}
});
t.Start();
Thread.Sleep(1000);
isStop = 1;
t.Join();
Console.WriteLine(" End of main thread execution ");
Console.ReadKey();
边栏推荐
- 高性能实战Alibaba Sentinel笔记,深度还原阿里微服务高并发方案
- 格力手机叫板苹果手机?除了嘴硬之外,恐怕再无其他
- 短文本重复率快速检测
- 【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
- What needs to be done for mobile app performance testing? How much is the performance test report charged?
- P3379 [template] nearest common ancestor (LCA)
- 什么是CAS 以及 CAS 中的 ABA 问题
- [deep learning 05] cross entropy loss function
- For the first time, the Pentagon admitted to funding 46 biological facilities in Ukraine. Russia once revealed that only three were safe
- [raise bar C #] how to call the base of the interface
猜你喜欢

Solve the problem that win10 virtual machine and host cannot paste and copy each other

【深度学习05】 交叉熵损失函数

【笔记】关于keil中的出现的编译映射内存不足的问题

Flutter drawer学习总结6

40 necessary methodologies for large factories

【操作教程】如何正确使用海康demo工具配置通道上线?

What happened when the legendary login prompt failed to connect to the server? How to solve it?
![[Multisim Simulation] differential amplifier circuit 2](/img/4e/f346a4e0e6171b4b7d8469ead7f250.png)
[Multisim Simulation] differential amplifier circuit 2

这些难搞的内存问题你都懂了吗?

2022危险化学品经营单位主要负责人考试题库及在线模拟考试
随机推荐
Application analysis of key recording and playing of wt2003h4-16s voice chip
618. How to prepare for the great promotion
Bottomnavigationview is used in conjunction with viewpager, to modify icon size, to remove text, etc
[Multisim Simulation] differential amplifier circuit 2
2022危险化学品经营单位主要负责人考试题库及在线模拟考试
《软件体系结构原理、方法与实践》第二版期末考试复习总结
What happened when the legendary login prompt failed to connect to the server? How to solve it?
[note] about the problem of insufficient compilation mapping memory in keil
这些难搞的内存问题你都懂了吗?
Leetcode-57- insert interval
高性能实战Alibaba Sentinel笔记,深度还原阿里微服务高并发方案
[operation tutorial] how to correctly use the Hikvision demo tool to configure the channel to go online?
Flutter drawer学习总结6
For the first time, the Pentagon admitted to funding 46 biological facilities in Ukraine. Russia once revealed that only three were safe
C#多线程学习笔记四
Win10 virtual machine download and installation process
【笔记】74HC573的一些记录
【C语言】指针函数与函数指针、数组函数
Typescript introductory notes (personal)
Kotlin 冒泡算法 ,高德地图 过滤掉两点之间距离小于50的数据,不重复显示