当前位置:网站首页>Replication of variables in golang concurrency
Replication of variables in golang concurrency
2022-06-24 04:41:00 【jerryteng】
Recently golang Programming language development , Deeply realized golang The convenience of language parallelism , But it also brings some small problems ,
No big problem , Let's solve . Let's first look at what concurrency is .
What is parallel ? What is concurrency ?
- The key to parallelism is that you can do many things at the same time , There are many common parallel computers , Multicore parallelism ; Concurrency is managing a lot of things at the same time , These things can be carried out within the specified time .
- Concurrency focuses on avoiding blocking , So that the program will not stop because of blocking ; Parallelism is dependent on hardware and operating system resources .
- Concurrency lies in structure , Parallelism lies in executing . The application has a good concurrent structure , The operating system can make better use of hardware parallel execution , Dispatch reasonably , promote CPU utilization .
golang in , We all talk about parallel , Try to tap the potential of the machine , Take full advantage of the multi-core nature of the machine , Make code execution most efficient .
golang The built-in functions of the language are used in parallel go , Let's start a collaborative process . This process is called goroutine
What is? goroutine?
goroutine Is a lightweight abstraction based on threads . stay Go In language , When a function is created as goroutine when ,Go Think of it as a separate unit of work , And it can execute multiple in parallel at a very low cost goroutine.
say , Today's problem ,
I'm in a loop , Set up a group goroutinue , Each coroutine executes a function , In this function ,
func (s *ServiceImpl) getData(
ctx context.Context,
aa int64,
where []string,
) (data Data,err error) {
queryWhere := where
I found that every request has a problem ,
Ten pieces of data will be queried each time , But every time you assemble SQL All have problems , There is a problem SQL It's also random .
So I print the log in the code to see ,
Find log output , It turned out to be normal
This is strange , Add a line of code for logging ,
Remove Log code , Another problem 100% Reappear .
Finally, various search problems , Suddenly found that ,queryWhere It's a slice ,
It is still a matter of basic knowledge ,golang in slice At the bottom is an array , And it is a reusable array ,
Change the code to
func (s *ServiceImpl) getData(
ctx context.Context,
aa int64,
where []string,
) (data Data,err error) {
queryWhere := make([]string, len(where))
copy(queryWhere, where)Do not add log statements , The problem has also been solved .
We still have to lay more foundation
边栏推荐
- ServiceStack. Source code analysis of redis (connection and connection pool)
- Are you ready for the exam preparation strategy of level II cost engineer in 2022?
- Summary of Android interview questions in 2020 (intermediate)
- 重新认识WorkPlus,不止IM即时通讯,是企业移动应用管理专家
- Library management backstage
- TCPIP协议详解
- 阿里云新一代云计算体系架构 CIPU 到底是啥?
- Beauty of script │ VBS introduction interactive practice
- Digital transformation practice of Zheshang Bank
- 15+ urban road element segmentation application, this segmentation model is enough
猜你喜欢

Recognize workplus again, not only im but also enterprise mobile application management expert

『应急响应实践』LogParser日志分析实践

15+ urban road element segmentation application, this segmentation model is enough

Doctor application | Hong Kong University of science and Technology (Guangzhou) Mr. Liu Hao recruits the full award doctor / Master in data mining

ServiceStack. Source code analysis of redis (connection and connection pool)

The results of the 2022 open source summer were announced, and 449 college students will contribute to open source projects

How can the new generation of HTAP databases be reshaped in the cloud? Tidb V6 online conference will be announced soon!

Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse

博士申请 | 香港科技大学(广州)刘浩老师招收数据挖掘方向全奖博士/硕士

Changjiang Dayong, director of openeuler community: jointly promote the new open source model of Euler and jointly build a new open source system
随机推荐
Recognize workplus again, not only im but also enterprise mobile application management expert
[2021 "shadow seeking" medical artificial intelligence algorithm competition] frequently asked questions related to Ti-One products
How does the compiler put the first instruction executed by the chip at the start address of the chip?
oracle数据库提示无操作权限的问题
近两周ACM之DP总结
梯度下降法介绍-黑马程序员机器学习讲义
Cadence OrCAD Capture 批量修改网络名称的两种最实用的方法图文教程及视频演示
Easyanticheat uses to inject unsigned code into a protected process (1)
Getattribute return value is null
Advanced authentication of uni app [Day12]
Spirit breath development log (16)
大一下学期期末总结(补充知识漏洞)
Application and related research of Worthington elastase
What if the ECS forgets its password? How can I retrieve my forgotten password?
Worthington胰蛋白酶的物化性质及特异性
How to build an ECS and how to control the server through the local host
SAP mts/ato/mto/eto topic 8: ATO mode 2 d+ empty mode strategy 85
uni-app进阶之认证【day12】
Web penetration test - 5. Brute force cracking vulnerability - (5) SMB password cracking
How to add a domain name to ECS? What are the advantages of ECS?