17

I'm working on a project to handle state machine changes that need to be obeyed between numerous thread but just stumbled onto they are nonatomic by default. Is there a way to make Swift properties thread-safe or atomic at the time of Xcode6-Beta4? Thanks in advance.

AJ_1310
  • 3,303
  • 3
  • 19
  • 26
  • See here: http://stackoverflow.com/questions/24157834/are-swift-variables-atomic – Widerberg Aug 04 '14 at 13:57
  • I did read that post but wanted to make sure since the answers written were of a previous Beta. Thanks – AJ_1310 Aug 04 '14 at 14:06
  • Since Apple hasn't released any "real" documentation right now, the whole Swift paradigm is to be seen as a beta. You can use `objc_sync_enter(self) //synchronized code objc_sync_exit(self)` to synchronize your code right now though. – Widerberg Aug 04 '14 at 14:12
  • Properties being atomic is virtually never enough to make code actually thread safe, so I would be surprised if Apple adds atomic properties. – drewag Aug 04 '14 at 14:54

2 Answers2

2

I think Alexander W has the right idea, but I would advise synchronizing on self as a general rule. Perhaps the suggestion I posted here may help:

Community
  • 1
  • 1
skim
  • 2,267
  • 2
  • 16
  • 17
1

You can implement your Getters Setters on thread-safe properties with use of dispatch_semaphore_t, NSLock or pthread_mutex_t, or similiar stuff.

Currently, there are no attributes, which define some behaviour, like atomic/nonatomic quialifers of Obj-C

Elijah Igny
  • 65
  • 1
  • 6