开发者

Objective-C, global variables and threads

开发者 https://www.devze.com 2023-03-01 08:17 出处:网络
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

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 variab开发者_如何学运维le belongs to it.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消