我的golang代码怎么了?

这是我的整个源代码:


package main


import (

    "sync/atomic"

    "unsafe"

    "sync"

    "fmt"

    "time"

    "runtime"

)


const (

    MAX_DATA_SIZE = 100

)


// lock free queue

type Queue struct {

    head unsafe.Pointer

    tail unsafe.Pointer

}

// one node in queue

type Node struct {

    val interface{}

    next unsafe.Pointer

}

// constructor

func New() (q *Queue) {

    queue := new(Queue)

    queue.head = unsafe.Pointer(new(Node))

    queue.tail = queue.head

    return queue

}

// queue functions

func (self *Queue) enQueue(val interface{}) {

    newValue := unsafe.Pointer(&Node{val: val, next: nil})

    var tail,next unsafe.Pointer

    for {

        tail = self.tail

        next = ((*Node)(tail)).next

        if atomic.CompareAndSwapPointer(&next, nil, newValue) {

            atomic.CompareAndSwapPointer(&self.tail, tail, newValue)

            break

        }else{

            for next != nil {

                tail = next

            }

        }

    }

}


func (self *Queue) deQueue() (val interface{}, success bool){

    var head,next unsafe.Pointer

    for {

        head = self.head

        next = ((*Node)(head)).next

        if next == nil {

            return nil, false

        }else {

            if atomic.CompareAndSwapPointer(&(self.head), head, next) {

                val = ((*Node)(next)).val

                return val, true

            }

        }

    }

    return nil, false

}


func main() {

    //runtime.GOMAXPROCS(runtime.NumCPU())

    fmt.Println(runtime.GOMAXPROCS(-1))

    var wg sync.WaitGroup

    wg.Add(20)

    queue := New()

    for i := 0; i < 10; i++ {

        go func() {

            defer wg.Done()

            for j := 0; j < MAX_DATA_SIZE; j++ {

                t := time.Now()

                fmt.Println("enqueue111")

                fmt.Println("enq = ", t)

                fmt.Println("enqueue222")

                queue.enQueue(t)

            }

        }()

    }


代码停留在fmt.Println(“ enq =”,t)处,但是我不知道为什么,这太奇怪了。


梵蒂冈之花
浏览 173回答 2
2回答

POPMUISE

我认为出队goroutine一直在运行,试图从空队列中出队,而入队goroutine却饿死了。也许尝试使出队阻塞来给入队运行的机会?如果这不仅仅是为了教育价值,我只会使用渠道。编辑:使用通道的等效版本package mainimport (&nbsp; &nbsp; "sync"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time"&nbsp; &nbsp; "runtime")const (&nbsp; &nbsp; MAX_DATA_SIZE = 100)func main() {&nbsp; &nbsp; runtime.GOMAXPROCS(runtime.NumCPU())&nbsp; &nbsp; fmt.Println(runtime.GOMAXPROCS(-1))&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; wg.Add(20)&nbsp; &nbsp; queue := make(chan interface{})&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for j := 0; j < MAX_DATA_SIZE; j++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t := time.Now()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queue <- t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }()&nbsp; &nbsp; }&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var val interface{}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for j := 0; j < MAX_DATA_SIZE; j++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val = <- queue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("deq = ",val)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }()&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go