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.
Asked
Active
Viewed 9,913 times
17
-
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 Answers
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:
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