2

I have been looking online at what a thread is and I do not feel like I understand it. Could someone shed some light on this? In terms of programming languages for relating to C++, objective-C would be nice.

In objective-c, I encountered

@property(nonatomic, strong) NSString *name;

the explanation for nonatomic was it means to not be worried about multiple threads trying to access the object at the same time, and objective-c does not have to synthesize thread safe code. So what does that exactly mean as well.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Masterminder
  • 1,127
  • 7
  • 21
  • 31
  • 1
    Does it suffice to say something that runs concurrently with other threads in your process? – chris Feb 14 '13 at 03:15
  • Code is executed one line at a time (which i'm sure you're familiar with). When you multithread, you are running multiple lines of code at once. For instance, the user might be scrolling a view while you are downloading a file in the background. Now if the user clicks a button they might be executing yet another bit of code. Say the button they clicked edits the same variable that your download function is currently editing. If something wasn't thread safe you would run into problems with this. With stuff that is thread save the OS will handle it gracefully. – endy Feb 14 '13 at 03:19
  • I think your understanding of nonatomic is backwards. look at this question: http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties – Gabriel Southern Feb 14 '13 at 03:23
  • Linking these questions would be useful: http://stackoverflow.com/questions/1096776/what-are-threads-what-is-a-thread?rq=1, http://stackoverflow.com/questions/775139/what-are-threads?rq=1 – jogojapan Feb 14 '13 at 03:24
  • @endy: "you are running multiple lines of code at once" - except that's not necessarily true (for the normal use of "at once")... you can have a multi-threaded single-core/cpu system that only executes one thread at any given moment in time. – Tony Delroy Feb 14 '13 at 03:25
  • @TonyD It was meant to be an over simplification. OP doesn't exactly seem like he was looking for the most detailed textbook response. – endy Feb 14 '13 at 19:15

2 Answers2

6

A process can consist of multiple threads of execution, which logically can be thought of as running simultaneously alongside each other. Each thread runs independently but shares the same memory and process state. A single thread can "do one thing": perform computation, interact with the network, update a UI, decode a video, etc. But, a single thread cannot do all of these at once without a significant amount of extra work from the programmer. Having multiple threads in a process enables the programmer to easily enable an application to do multiple things at once (multitasking).

Using multiple threads does introduce some new challenges, though. For example, if you have two threads that access the same variable, you can end up with a concurrency hazard in which the variable might not be completely updated by one thread before the other thread accesses it, leading to failures in the program. Objective-C will generate thread-safe code by default, to avoid this situation. nonatomic tells the compiler that you will never access it from multiple threads simultaneously, so the compiler can skip the thread-safe code and make faster code. This can be useful if you are going to supply your own synchronization, anyway (e.g. to keep a group of properties in sync, which Objective-C itself cannot help you with).

If you violate the core nonatomic assumption and access a nonatomic variable from multiple threads at the same time, all hell will break loose.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 4
    atomic properties doesn't mean thread-safe code! That's a huge misconception, it just means that your property will get and set in atomic manner! Proper synchronization is, in most cases, done on a different level and the getter/setter has no influence about it. – JustSid Feb 14 '13 at 03:38
  • 1
    Objective-C generates thread-safe code for accessing the properties, nothing more (or less). This ensures that the property itself is in a consistent state. It does not guarantee that multiple properties are all in a coherent state; for that, you need external synchronization. – nneonneo Feb 14 '13 at 03:39
5

explanation for nonatomic was it means to not be worried about multiple threads trying to access the object at the same time, and objective-c does not have to synthesize thread safe code. So what does that exactly mean as well.

Imagine you are asked to write your name on a piece of paper. You're given a list of instructions someone thought would work just fine:

  • you find a line that's current empty,
  • move your pen over it,
  • write your name.

All good.

Now imagine you're given a new piece of paper, but both you and someone else are asked to write your names on the same piece of paper, and you're given the old instructions, perhaps:

1) You both look at the paper and determine to write on the first line. 2) You put your pens down (maybe you can both do it comfortably enough - one left / one right handed). 3) You start to write an I but the other person is writing a J and it comes out looking like a U. 4) gets worse from here....

But equally, it might be that you're paying more attention, and finish writing your name before they start looking for an empty line, or vice versa.

Threading is a lot like this... in the above example, each thread/person is keeping track of how they're progressing at the task, following their instructions very literally. Notice that if you complete only step 1, then the other person does step 1, you've already set yourselves up to write over each others' name regardless of the ordering or concurrency of the remaining steps.

In all this, you don't even have to be doing things at the same instant in time, it's just that the tracking of your tasks is independent - you're independent people with your own memory of where you are in your task. Same with threads - they're ways of tracking what to do independently, and it's optional whether they actually do things in your program at the same instant (which is possible with multi-core CPUs and multi-CPU systems).

"atomic" is used in the sense of indivisible (think: you can't cut an atom of gold in half and still have gold). Similarly, if you say write your name atomically, it means any observer is guaranteed to either witness the instant before - when no name is there - or the instant after - when your name is completely written - but they'll never see just half your name. An atomic update on a string variable is like that.

Atomic string updates don't solve the problem above... you might still clash in finding "an empty line" (in a computing context - say finding the next empty position in a container). If that process of finding an empty line is atomic, and the line is somehow marked "used" even before you've written anything on it yourself, then it means you'll never get the same line as someone else. At that stage, multiple people writing their names won't clash on the same line, but only when both the line finding and the name writing are atomic can people looking at the paper know that they're seeing completely written non-clashing names.

Making these kind of guarantees is very useful but expensive. It means that threads must communicate and coordinate amongst themselves, agreeing "who" will go first with others waiting as necessary.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252