新建一个view-based application,
viewcontroller.h的代码如下:
#import <UIKit/UIKit.h>
@interface tConditionViewController : UIViewController {
int tickets;
NSThread *t1;
NSThread *t2;
NSCondition *tc;
}
-(void)doSomething:(id)tname;
@end
viewcontroller.m的代码如下:
#import "tConditionViewController.h"
@implementation tConditionViewController
-(void)doSomething:(id)tname
{
NSString *name = (NSString *)tname;
while (TRUE) {
NSLog(@"in thread %@", name);
[NSThread sleepForTimeInterval:1];
[tc lock];
NSLog(@"kkkk");
[tc unlock];
}
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
tc = [[NSCondition alloc] init];
t1 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:@"1"];
[t1 start];
t2 = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:@"2"];
[t2 start];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
分享到:
相关推荐
iOS线程同步方案,包括各种互斥锁,自旋锁,递归锁等深度解析!!!!
本实例写了一个多线程,从中您可以学到线程创建,线程同步,等基本知识。
iOS线程安全,读写锁,栅栏函数等深度解析,多读单写保证公共资源安全!
ios多线程编程指南,包含NSThread,CGD等,深入简出讲解ios多线程原理和应用
iOS 线程资料汇总 iOS 线程使用例子及iOS并发编程指南及代码
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD
这是一篇介绍IOS多线程理解的文章,简洁易懂,方便阅读
关于iOS 多线程编程知识点,iOS 多线程 NSThread NSOperation GCD
iOS实现类似安卓的加锁效果; 欢迎提出更好的建议
iOSGCD控制多线程任务数量,直接使用ZKRGlobalQueue^()。
iOS多线程面试题
iOS多线程编程指南!!
iOS开发学习之iOS多线程和RunLoop.pdf
iOS开发多线程,NSoperetion, GCD,多线程的网络图片的下载
ios_app资源
ios 多线程NSoperation
(纯原创)iOS实现类似安卓的手势加锁效果~~~欢迎提意见
iOS多线程,四种实现方式中最简单的两种pthread,NSThread的用法介绍。
iOS常见的多线程:GCD 任务和队列,异步执行 + 并发队列,线程锁,信号量
IOS线程学习文档+实例代码,特别适合新手学习,超好