Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.11 KB | None | 0 0
  1. package main
  2. import (
  3.     "context"
  4.     "fmt"
  5.     "sync"
  6.     "sync/atomic"
  7.     "time"
  8. )
  9.  
  10. var mutex = &sync.Mutex{}
  11.  
  12. type Fib struct {
  13.     a int64
  14.     b int64
  15.     timeStart int64
  16.     timeEnd int64
  17. }
  18.  
  19. func withMutex(ctx context.Context, fibbValue *Fib) {
  20.     var a int64 = 0
  21.     var b int64 = 1
  22.     var aku int64 = 0
  23.  
  24.     for {
  25.         select {
  26.         case <-ctx.Done():
  27.             return
  28.         default:
  29.             mutex.Lock()
  30.             a = fibbValue.a
  31.             b = fibbValue.b
  32.             mutex.Unlock()
  33.             aku = b
  34.             b = a + b
  35.             a = aku
  36.  
  37.             if (b < 0) {
  38.                 mutex.Lock()
  39.                 timeNow := time.Now().UnixNano()
  40.                 fibbValue.timeEnd = timeNow - fibbValue.timeStart
  41.                 mutex.Unlock()
  42.                 return
  43.             }
  44.  
  45.             mutex.Lock()
  46.             fibbValue.a = a
  47.             fibbValue.b = b
  48.             mutex.Unlock()
  49.         }
  50.     }
  51. }
  52.  
  53. func withAtomic(ctx context.Context, fibbValue *Fib) {
  54.     var a int64 = 0
  55.     var b int64 = 1
  56.     var aku int64 = 0
  57.  
  58.     for {
  59.         select {
  60.         case <-ctx.Done():
  61.             return
  62.         default:
  63.             a = atomic.LoadInt64(&fibbValue.a)
  64.             b = atomic.LoadInt64(&fibbValue.b)
  65.             aku = b
  66.             b = a + b
  67.             a = aku
  68.  
  69.             if (b < 0) {
  70.                 mutex.Lock()
  71.               timeNow := time.Now().UnixNano()
  72.               fibbValue.timeEnd = timeNow - fibbValue.timeStart
  73.               mutex.Unlock()
  74.                 return
  75.             }
  76.  
  77.             atomic.StoreInt64(&fibbValue.a, a)
  78.             atomic.StoreInt64(&fibbValue.b, b)
  79.         }
  80.     }
  81. }
  82.  
  83. func main() {
  84.     ctx := context.Background()
  85.     ctxAtomic, cancelAtomic := context.WithCancel(ctx)
  86.    
  87.     timeNow := time.Now().UnixNano()
  88.     fibbValue := Fib{a: 0, b: 1, timeStart: timeNow}
  89.  
  90.     go withAtomic(ctxAtomic, &fibbValue)
  91.     go withAtomic(ctxAtomic, &fibbValue)
  92.     go withAtomic(ctxAtomic, &fibbValue)
  93.     go withAtomic(ctxAtomic, &fibbValue)
  94.     time.Sleep(100 * time.Millisecond)
  95.     cancelAtomic()
  96.     fmt.Println("Done Atomic. Time [ns]:", fibbValue.timeEnd)
  97.  
  98.     ctxMutex, cancelMutex := context.WithCancel(ctx)
  99.  
  100.     timeNow = time.Now().UnixNano()
  101.     fibbValue = Fib{a: 0, b: 1, timeStart: timeNow}
  102.  
  103.     go withMutex(ctxMutex, &fibbValue)
  104.     go withMutex(ctxMutex, &fibbValue)
  105.     go withMutex(ctxMutex, &fibbValue)
  106.     go withMutex(ctxMutex, &fibbValue)
  107.     time.Sleep(100 * time.Millisecond)
  108.     cancelMutex()
  109.     fmt.Println("Done Mutex. Time [ns]:", fibbValue.timeEnd)
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement