1

I am setting a variable in a thread, and I'm checking using it for a condition in another thread.

I'm aware of the race condition here. But not sure if doing the set in atomic way will be enough.

Can you please let me know how to solve this in objective-c.

UPDATE: Please also let me know how to do it for a global variable.

coder000001
  • 476
  • 4
  • 22

2 Answers2

1

Many solutions to this generalized problem have been proposed over the last 50 years. :)

Changing the setter semantics to atomic will NOT solve the problem---that simply prevents changes from occurring during the getting and setting of your variable (see the Objective-C Documentation for what it actually does).

What you want is a signaling mechanism between threads. Check out Objective-C Conditions.

0

If you define it as a property, use the atomic keyword. Or just leave out the nonatomic keyword, since atomic is the slower default.

@property (atomic, retain) NSString *value;

or just

@property (retain) NSString *value;
picciano
  • 22,341
  • 9
  • 69
  • 82