Line Cheng security Problem of Go language map map

Source: Internet
Author: User
Tags serialization
See comments in the code:

Main

(
       "FMT"
       "Sync"
)

Main ()  {

       map[string]int{"a": 1 , "B": 2, "C": 3, "D": 4, "E": 5, "F": 6}

       FMT. Println (m)
       FMT. Println ("-------")

       WG: = &sync. waitgroup{}
       WG. ADD (3)
       go funcmap[string]int] {

              WG. Done ()
              p["a"] = Ten

       } (m)// because the map in the Go language is a reference type, although the Go language function is called by value, that is, the function holds a copy of the parameter, but because it is a reference type, it still points to the map m, a pointer like C.
       go funcmap[String]int] {

              WG. Done ()
              p["a"] = one

       } (m)
       go funcmap[string]int) {

              WG. Done ()
              p["a"] =

       (m)

       WG. Wait ()

       FMT. Println (M)
/*
Because the map is a reference type, even if the function is called, the parameter copy still points to the mapping m, so 3 goroutine concurrently write the same mapping m, and the classmates who write too many threads know that for shared variables, resources,
Concurrent read and write will produce competition, so the shared resources are destroyed, so either lock, or use channel queuing serialization, in short, to exclusive access.
So remember: Many goroutine read and write the same mapping, to protect yo, locking can also, using channel serialization processing can also ...
*/
}
Note: This article is only my personal notes, if there are errors and omissions, please correct me, study together, my e-mail: htyu_0203_39@sina.com
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.