Golang map data structure does not have concurrent read and write problems

Source: Internet
Author: User
Tags map data structure
This is a creation in Article, where the information may have evolved or changed.
error: concurrent map writes

Have you ever met this error?

Origin of the problem

Today, I encountered a problem in the coding concurrency test process that directly kills the entire process. We know that as long as there is a goroutine happening in the Golang panic the whole process is hung up. At that time, a face Meng than. Begins checking stack information.

Problem analysis

At first I looked at the relevant solution. Mostly multithreaded operation map data structure must be locked. Otherwise this error must be present. I look at my code, I think I wrote the map structure has been added lock, additional lock mode:

    1. Universal Lock
typestruct {  map[string]string   Lock sync.Mutex}funcstringstring{  d.Lock.Lock()  defer d.Lock.UnLock()  return d.Data[k]}funcstring) {  d.Lock.Lock()  defer d.Lock.UnLock()  d.Data[k]=v}
    1. Read/write Lock
typestruct {  map[string]string   Lock sync.RwMutex}funcstringstring{  d.Lock.RLock()  defer d.Lock.RUnlock()  return d.Data[k]}funcstring) {  d.Lock.Lock()  defer d.Lock.UnLock()  d.Data[k]=v}

The above is a simple example of a concurrent operation map. So that terrible results would not have happened.

SB.

Before I said that the map is no problem, then why I met this problem. That's because my own HTTP framework gives big bugs, and multiple threads operate on the same action instance. Ggggg.
If you are experiencing this problem, check your map for multi-threading at once
This article source: Golang map data structure can not be read and write problems

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.