Matthew Mitchell
Matthew Mitchell

Reputation: 5383

Objective-C, global variables and threads

I've made several Objective-C class files. Two of them had the same name for a global variable. When the program was running a thread from one file but was also accessing code from the other file on another thread, the thread seemed to get confused on what global variable belongs to it.

Is this a true issue or was my code wrong? I seem to have fixed it by changing the variable name.

Upvotes: 0

Views: 716

Answers (1)

Aurojit Panda
Aurojit Panda

Reputation: 909

I would go with your code was wrong, but I think there's a more fundamental thing you are misunderstanding here.

A thread doesn't per-se belong to a file, or own anything. What really happened is say you have two functions, one in each of your file, the compiler (since your variables aliased each other) chose to use one variable in one file, and another in another. This has nothing to do with threads, or anything else.

Furthermore, if you are looking for a thread local variable, you might want to look at the threadDictionary property of NSThread http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html

Upvotes: 3

Related Questions