google / btree

BTree provides a simple, ordered, in-memory data structure for Go programs.
Apache License 2.0
3.9k stars 414 forks source link

Worked unexpected in go rountine? #36

Closed funlake closed 3 years ago

funlake commented 3 years ago

Wrote some test codes but got fatal error

    bt := btree.New(32)
    var wg sync.WaitGroup
    //var lock sync.Mutex
    for i:=1;i<10000;i++ { 
        wg.Add(1)
        go func(n int) {
            //lock.Lock()
            bt.ReplaceOrInsert(btree.Int(n))
            bt.Delete(btree.Int(n))
            //lock.Unlock()
            wg.Done()
        }(i)
    }
    wg.Wait()
    t.Log(bt.Len())
    bt.Clear(true)

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x5264b1]

goroutine 94 [running]: github.com/google/btree.items.find(0xc000050010, 0x1, 0x1, 0x7a6820, 0xc00008e1e0, 0x0, 0x0) /mnt/d/develop/go/pkg/mod/github.com/google/btree@v1.0.0/btree.go:191 +0xc1

funlake commented 3 years ago

Well i see it's threadunsafe