-2

可能重复:
为什么我的 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)
            }
        }()
    }

    for i := 0; i < 10; i++ {
        go func() {
            ok := false
            var val interface{}
            defer wg.Done()
            for j := 0; j < MAX_DATA_SIZE; j++ {
                val,ok = queue.deQueue()
                for !ok {
                    val,ok = queue.deQueue()
                }
                fmt.Println("deq = ",val)
            }
        }()
    }

    wg.Wait()
}

代码卡在fmt.Println("enq = ", t)但我不知道为什么,这太奇怪了。

4

2 回答 2

1

我认为出队 goroutines 一直在运行,试图从一个空队列中出队,而入队 goroutines 却饿死了。也许尝试使出队阻塞以使入队有机会运行?如果这不仅仅是为了教育价值,我会使用channels

编辑:使用渠道的等效版本

package main

import (
    "sync"
    "fmt"
    "time"
    "runtime"
)

const (
    MAX_DATA_SIZE = 100
)

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    fmt.Println(runtime.GOMAXPROCS(-1))
    var wg sync.WaitGroup
    wg.Add(20)
    queue := make(chan interface{})
    for i := 0; i < 10; i++ {
        go func() {
            defer wg.Done()
            for j := 0; j < MAX_DATA_SIZE; j++ {
                t := time.Now()
                queue <- t
            }
        }()
    }

    for i := 0; i < 10; i++ {
        go func() {
            var val interface{}
            defer wg.Done()
            for j := 0; j < MAX_DATA_SIZE; j++ {
                val = <- queue
                fmt.Println("deq = ",val)
            }
        }()
    }

    wg.Wait()
}
于 2012-09-07T10:25:52.257 回答
1

deQueue 在失败情况下无限循环,这会阻塞 CPU。Goroutines 在做 CPU 工作时不会屈服。GOMAXPROCS 需要 >= 2 才能获得 CPU 并行度。

只是为了好玩,这是一个使用高阶通道的线程安全、非阻塞队列实现:https ://gist.github.com/3668150

于 2012-09-07T18:04:55.243 回答