---
# System prepended metadata

title: Untitled

---

package main

import (
    "sync/atomic"
    "time"
)

// how to do a lock free
type memcache struct {
    members []string
    readc   chan chan []string
    readb  chan chan []int
    reada  chan chan []bool
    readf
}

func (m *memcache) Start(ctx) {
    go func() {
        for {
            select {
                case ch := <-readc:
                // blalbbla
                // read old data
            }
        }
    }
    go func(){
      for {
         select {
           case <-ticker.C:
               bm := m.fetchUpdate()
               am := m.members 
               atomic.Swap(am, bm)
               // m.members = m.fetchUpdate()
         }
      }
    }

}

func (m *memcache) Read() {
    resc := make(chan []string)
    m.readc <- resc
    <-resc
}

func main() {
    members := []*memcache{
        &memcache{
           name:"lindroos" ,
           age:99,
        },
        &memcache{
           name:"wys1203" ,
           age:18,
        },
    }
    
    ctx := context.Background()
    
    go func() {
        m.Start(ctx)
    }
    
    ch := make(chan string, 10)
    go func(){
        for {
            select {
                newlist <- ch:
                // slice memory address swap to new memory
                
                members = newlist
            }
        }
    }
    
    go func (){
        ch <- []*memcache{
        &memcache{
           name:"lindroos" ,
           age:99,
        },
        &memcache{
           name:"wys1203" ,
           age:18,
        },
    }
        // without lock
        // next
    }
    
    go func() {
       ch <- "gglong" 
    }
    
    time.Sleep(10* time.Second)
    fmt.Println(members[0].name)
}