#import <Foundation/Foundation.h>@interface MainUI : NSObject
{
NSLock* _lock;
}-(void)initialize;@property(nonatomic, retain) NSLock* _enterEmShopLock;
@property(atomic, assign) volatile BOOL _bEnterEmShop;@end#import "MainUI.h"@implementation MainUI@synthesize _bEnterEmShop=bEnterEmShop;
@synthesize _enterEmShopLock=enterEmShopLock;-(void)initialize
{
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
[NSThread mainThread].name = @"Main";
NSLog(@"main thread name is %@", [[NSThread mainThread]name]);
bEnterEmShop = YES;
[enterEmShopLock lock];
NSLog(@"cur thread name is %@ and enter lock", [[NSThread currentThread]name]);
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
NSLog(@"cur thread name is %@ and exit unlock", [[NSThread currentThread]name]);
[enterEmShopLock unlock];
}-(void)run:(MainUI*)mainUI
{
[NSThread currentThread].name = @"Sub";
if(!mainUI._bEnterEmShop)
{
NSLog(@"cur thread name is %@", [[NSThread currentThread]name]);
[[mainUI _enterEmShopLock]lock];
NSLog(@"cur thread name is %@ and enter lock", [[NSThread currentThread]name]);
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
NSLog(@"cur thread name is %@ and exit unlock", [[NSThread currentThread]name]);
[[mainUI _enterEmShopLock]unlock];
}
else {
NSLog(@"cur thread name is %@ and can't be allowed to do anything", [[NSThread currentThread]name]);
}
}@end出现的结果:
2013-10-13 16:50:41.368 ThreadSynPrj[18231:f803] main thread name is Main
2013-10-13 16:50:41.368 ThreadSynPrj[18231:1290b] cur thread name is Sub
2013-10-13 16:50:41.370 ThreadSynPrj[18231:f803] cur thread name is Main and enter lock
2013-10-13 16:50:41.370 ThreadSynPrj[18231:1290b] cur thread name is Sub and enter lock
2013-10-13 16:50:43.372 ThreadSynPrj[18231:f803] cur thread name is Main and exit unlock
2013-10-13 16:50:43.372 ThreadSynPrj[18231:1290b] cur thread name is Sub and exit unlock为什么主线程没有退出,子线程又进来了?NSLock不是互斥锁嘛?
请高手指导,谢谢!NSLock ios同步 多线程 锁
{
NSLock* _lock;
}-(void)initialize;@property(nonatomic, retain) NSLock* _enterEmShopLock;
@property(atomic, assign) volatile BOOL _bEnterEmShop;@end#import "MainUI.h"@implementation MainUI@synthesize _bEnterEmShop=bEnterEmShop;
@synthesize _enterEmShopLock=enterEmShopLock;-(void)initialize
{
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
[NSThread mainThread].name = @"Main";
NSLog(@"main thread name is %@", [[NSThread mainThread]name]);
bEnterEmShop = YES;
[enterEmShopLock lock];
NSLog(@"cur thread name is %@ and enter lock", [[NSThread currentThread]name]);
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
NSLog(@"cur thread name is %@ and exit unlock", [[NSThread currentThread]name]);
[enterEmShopLock unlock];
}-(void)run:(MainUI*)mainUI
{
[NSThread currentThread].name = @"Sub";
if(!mainUI._bEnterEmShop)
{
NSLog(@"cur thread name is %@", [[NSThread currentThread]name]);
[[mainUI _enterEmShopLock]lock];
NSLog(@"cur thread name is %@ and enter lock", [[NSThread currentThread]name]);
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
NSLog(@"cur thread name is %@ and exit unlock", [[NSThread currentThread]name]);
[[mainUI _enterEmShopLock]unlock];
}
else {
NSLog(@"cur thread name is %@ and can't be allowed to do anything", [[NSThread currentThread]name]);
}
}@end出现的结果:
2013-10-13 16:50:41.368 ThreadSynPrj[18231:f803] main thread name is Main
2013-10-13 16:50:41.368 ThreadSynPrj[18231:1290b] cur thread name is Sub
2013-10-13 16:50:41.370 ThreadSynPrj[18231:f803] cur thread name is Main and enter lock
2013-10-13 16:50:41.370 ThreadSynPrj[18231:1290b] cur thread name is Sub and enter lock
2013-10-13 16:50:43.372 ThreadSynPrj[18231:f803] cur thread name is Main and exit unlock
2013-10-13 16:50:43.372 ThreadSynPrj[18231:1290b] cur thread name is Sub and exit unlock为什么主线程没有退出,子线程又进来了?NSLock不是互斥锁嘛?
请高手指导,谢谢!NSLock ios同步 多线程 锁
解决方案 »
- ipad当前页面显示多个tableview怎么实现
- UITextView becomeFirstResponder即弹出键盘
- UItableView如何调整位置
- 请问开发iphone用Mac Mini可以吗?
- 给黑苹果搞个Navicat for SQLite 求个下载地址
- EGORefreshTableHeaderView 下拉刷新
- 请问 我的内存是2个 英特尔双核E2200可以安装苹果mac os等吗
- 我想给公司开发应用,能不能不用发布到iTunes上?
- 百度地图真机测试问题,求大神解答
- 新人求助,关于table view的这种蓝色的勾怎么做
- .net转ios开发,公司需要没办法,希望有人能把经验分享一下,谢谢csdn平台!
- 如何实现在苹果手机和外界碰撞后再执行某项操作
[NSThread mainThread].name = @"Main";
NSLog(@"main thread name is %@", [[NSThread mainThread]name]);
bEnterEmShop = YES;
enterEmShopLock = [[NSLock alloc]init];
[enterEmShopLock lock];
NSLog(@"cur thread name is %@ and enter lock", [[NSThread currentThread]name]);
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
NSLog(@"cur thread name is %@ and exit unlock", [[NSThread currentThread]name]);
[enterEmShopLock unlock];
头文件忘记初始化了,不过这样也还是不行,还是会出现上述问题,不知道什么原因!