Advertisement
JetForMe

Untitled

Nov 22nd, 2016
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.87 KB | None | 0 0
  1. public
  2. struct
  3. AtomicInteger : ExpressibleByIntegerLiteral
  4. {
  5.     fileprivate let     queue = DispatchQueue(label: "AtomicInteger Queue", qos: .userInteractive)
  6.     fileprivate var     internalValue: Int
  7.    
  8.     var             value: Int
  9.     {
  10.         get
  11.         {
  12.             var result: Int = 0
  13.             self.queue.sync
  14.             {
  15.                 result = self.internalValue
  16.             }
  17.             return result
  18.         }
  19.     }
  20.    
  21.     public
  22.     init(_ inValue: Int)
  23.     {
  24.         self.internalValue = inValue
  25.     }
  26.    
  27.     public
  28.     init(integerLiteral inValue: Int)
  29.     {
  30.         self.internalValue = inValue
  31.     }
  32.    
  33.    
  34. }
  35.  
  36. func
  37. +=(_ inLHS: inout AtomicInteger, _ inRHS: Int)
  38. {
  39.     inLHS.queue.sync
  40.     {
  41.         inLHS.internalValue += inRHS
  42.     }
  43. }
  44.  
  45. func
  46. -=(_ inLHS: inout AtomicInteger, _ inRHS: Int)
  47. {
  48.     inLHS.queue.sync
  49.     {
  50.         inLHS.internalValue -= inRHS
  51.     }
  52. }
  53.  
  54. extension
  55. Int
  56. {
  57.     init(_ inValue: AtomicInteger)
  58.     {
  59.         self.init(inValue.value)
  60.     }
  61. }
  62.  
  63. var a = AtomicInteger(4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement