Threads Basics (static, class methods, and singletons)

Class methods are shared by threads

All methods who run a class method on a class name are using the same class and running through that one method. That class method acts as an entry point. You do this by looking at the address of self.

If you run couple of threads through this classMethod, you should something like this:

classMethod – thread B, self address is: 0x107159380
classMethod – thread A, self address is: 0x107159380
classMethod – thread C, self address is: 0x107159380

They all have the same address for ‘self’. Which means there exist only one blueprint of the class in which these class methods belong to.

static variables are shared by threads

you’ll see that in the output, they all display the static variable’s address, which is the same for all threads:

classMethod – thread B, local static int number’s address is: 0x1071594e8
classMethod – thread C, local static int number’s address is: 0x1071594e8
classMethod – thread A, local static int number’s address is: 0x1071594e8

local variables are not shared. They belong to their own thread stack

you’ll see from output the the addresses of the local variable is different, which tells us that each thread have their own local variables:

classMethod – thread D, local int number’s address is: 0x11420fc44
classMethod – thread B, local int number’s address is: 0x114109c44
classMethod – thread C, local int number’s address is: 0x11418cc44
classMethod – thread F, local int number’s address is: 0x114315c44
classMethod – thread E, local int number’s address is: 0x114292c44

non-static variables are not allowed

If we were to declare an int number in our class:

and you try to use it in your static method, you’d get an error. Because only static variables are used in static methods. Its not intuitive or possible for a class method to keep track of instantiated variables. It simply does not work this way.

the variable ‘number’ is connected to an instantiation (object) of SomeClass.

No static class variables

You cannot declare ‘number’ in our previous example to be static because in objective c, there are no class static variables.

However, you CAN declare a static variable globally and use it in SomeClass’s static method. That is the basis of how a lot of singletons are made.

Example Singleton

.h

.m

As you notice we don’t let others copy or allocate our singleton by returning self. ….because there can be only 1 singleton.

Full Example

SomeClass header

SomeClass implementation

main – appdelegate

Result

classMethod2 – thread D, self address is: 0x107159380
classMethod – thread B, self address is: 0x107159380
classMethod – thread A, self address is: 0x107159380
classMethod – thread C, self address is: 0x107159380
classMethod2 – thread E, self address is: 0x107159380
classMethod2 – thread F, self address is: 0x107159380
classMethod2 – thread D, local static int number’s address is: 0x1071594ec
classMethod – thread B, local static int number’s address is: 0x1071594e8
classMethod – thread C, local static int number’s address is: 0x1071594e8
classMethod – thread A, local static int number’s address is: 0x1071594e8
classMethod2 – thread F, local static int number’s address is: 0x1071594ec
classMethod2 – thread E, local static int number’s address is: 0x1071594ec
classMethod – thread A, local int number’s address is: 0x114086c44
classMethod2 – thread D, local int number’s address is: 0x11420fc44
classMethod – thread B, local int number’s address is: 0x114109c44
classMethod – thread C, local int number’s address is: 0x11418cc44
classMethod2 – thread F, local int number’s address is: 0x114315c44
classMethod2 – thread E, local int number’s address is: 0x114292c44

Leave a Reply