dongwo1914 2016-01-21 14:51
浏览 33
已采纳

相当于GCD串行调度队列

Is there Go equivalent of Apple's GCD serial dispatch queue?

So far I have only found a solution that is a channel of functions.

work := make(chan func()) 

I would have a function receive from this channel and call the received functions. The functions must be executed in FIFO order.

Is there a better method or structure to do this in Go?

This shouldn't make a difference but I am looking to queue SQL queries to run in FIFO for this.

  • 写回答

2条回答 默认 最新

  • duannaoben8011 2016-02-11 19:36
    关注

    @OneOfOne, it was close but not quite.

    I ended up making a Serial Dispatch Queue implementation in Go available here.

    It is basically a go routine that blocks on a channel of type func() and runs the functions that are passed in order.

    Implementation:

    //Package serialqueue provides a serial queue for functions. 
    //Queue items are processed in First In First Out (FIFO) order. 
    package serialqueue
    
    //New returns a new serial queue.
    //Enqueue items like queueObj <- func() {doWork(data)}
    func New() chan func() {
        //create channel of type function
        var queue = make(chan func())
    
        //spawn go routine to read and run functions in the channel
        go func() {
            for true {
                nextFunction := <-queue
                nextFunction()
            }
        }()
    
        return queue
    }
    

    Usage: (demonstrating writing to a string in the correct order)

    //Package serialqueue provides provides tests for github.com/ansonl/serialqueue. 
    package serialqueue_test
    
    import (
        "testing"
        "fmt"
        "sync"
        "github.com/ansonl/serialqueue"
        )
    
    func TestQueue(t *testing.T) {
        //Create new serial queue
        queue := serialqueue.New()
    
        //Number of times to loop
        var loops = 100
    
        //Queue output will be added here
        var queueOutput string
    
        //WaitGroup for determining when queue output is finished
        var wg sync.WaitGroup
    
        //Create function to place in queue
        var printTest = func(i int) {
            queueOutput = fmt.Sprintf("%v%v",queueOutput, i)
            wg.Done()
        }
    
        //Add functions to queue
        var i int;
        for i=0;i<loops;i++ {
            wg.Add(1)
            t:=i
            queue <- func() {printTest(t)}
        }
    
        //Generate correct output
        var correctOutput string
        for i=0;i<loops;i++ {
            correctOutput = fmt.Sprintf("%v%v", correctOutput, i)       
        }
    
        //Wait until all functions in queue are done
        wg.Wait()
    
        //Compare queue output with correct output
        if queueOutput != correctOutput {
            t.Errorf("Serial Queue produced %v, want %v", queueOutput, correctOutput);
        }
    }
    

    Hope this helps someone with the same issue!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来