douyin2435 2018-05-19 08:11
浏览 111

如何在Go中将time.Time变量转换为atomic?

In my RESTFUL web service which is an online game, I'm storing starting time of every question in an global variable like this: var MyTime time.Time which I should update it after every level of the game. My application is distributed, so I want to make sure all of my apps are not updating it at the same time. That's why I've decided to make it atomic. Actually I'm familiar with Golang sync/atomic package. I tried to use atomic.LoadPointer() method but it needs specific argument type which isn't safe. Do you any other way for this?

Update: Okay I solved my problem like this. I defined time variable as atomic.Value and used atomic Load and Store methods. This is the code: var myTime atomic.Value myTime.Store(newTime) and load myTime.Load().(time.Time).

Consider that Load() method returns interface, so you should write (time.Time) at the end in order to convert it to time.Time type.

  • 写回答

2条回答 默认 最新

  • dongzhuo8210 2018-05-19 08:22
    关注

    This can't be done, as such, because time.Time is a compound type:

    type Time struct {
        // wall and ext encode the wall time seconds, wall time nanoseconds,
        // and optional monotonic clock reading in nanoseconds.
        //
        // From high to low bit position, wall encodes a 1-bit flag (hasMonotonic),
        // a 33-bit seconds field, and a 30-bit wall time nanoseconds field.
        // The nanoseconds field is in the range [0, 999999999].
        // If the hasMonotonic bit is 0, then the 33-bit field must be zero
        // and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext.
        // If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit
        // unsigned wall seconds since Jan 1 year 1885, and ext holds a
        // signed 64-bit monotonic clock reading, nanoseconds since process start.
        wall uint64
        ext  int64
    
        // loc specifies the Location that should be used to
        // determine the minute, hour, month, day, and year
        // that correspond to this Time.
        // The nil location means UTC.
        // All UTC times are represented with loc==nil, never loc==&utcLoc.
        loc *Location
    }
    

    However, you can do this with pointers, so *time.Time would be possible, if this suits your needs. But of course, this is discouraged, by virtue of the fact that atomic.LoadPointer and atomic.StorePointer use the unsafe package to accomplish their magic.

    A much better approach, if it will work for you, is just to use a mutex to protect your value. There are many ways to do this, but one minimal example:

    type MyTime struct {
        t  time.Time
        mu sync.RWMutex
    }
    
    func (t *MyTime) Time() time.Time {
        t.mu.RLock()
        defer t.mu.RUnlock()
        return t.t
    }
    
    func (t *MyTime) SetTime(tm time.Time) {
        t.mu.Lock()
        defer t.mu.Unlock()
        t.t = tm
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真