1
我無法找到任何實例如何處理相同的(類)變量當使用操作隊列。在C &線程它關於互斥體。所以,當NSOperationQueue
啓動一個線程操作和類變量被修改時會發生什麼?線程安全嗎?謝謝。線程安全:NSOperationQueue + [陣列ADDOBJECT]
@interface MyTest {
NSMutableArray *_array;
}
@end
-(id)init
{
...
_array = [NSMutableArray new]; // class variable
// queue time consuming loading
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation =
[NSInvocationOperation initWithTarget:self
selector:@selector(populate)
object:nil];
[queue addOperation:operation];
// start continuous processing
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(processing)
userInfo:nil
repeats:YES];
...
}
-(void)populate
{
while (...)
{
id element = ...; // time consuming
// modify class variable "_array" from operation's thread (?)
[_array addObject:element];
// Ok, I can do instead of addObject
// performSelectorOnMainThread:withObject:waitUntilDone:
// but is it the only way? Is it needed?
}
}
// access and/or modify class variable "_array"
-(void)processing
{
NSLog(@"array.count = %d", array.count);
for (id i in _array)
{
[_array addObject:[NSNumber numberWithInt:rand() % 100]];
// etc...
}
}
好吧,這是顯而易見的。爲什麼這不是蘋果公司的網站上寫的ConcurrencyProgrammingGuide(一個有關Operations /隊列)?我的意思是,這是我第一次看到'@ synchronized'。我現在看到,它在關於線程的部分中進行了描述。 – debleek63 2011-12-22 00:03:47
你認爲什麼是更好的做法:使用'@ synchronized'或調用'performSelectorOnMainThread'? – debleek63 2011-12-22 00:05:28
另一件事:是否聲明/訪問'_array'爲'atomic'屬性? – debleek63 2011-12-22 00:07:15