macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

isMainThread

現在のスレッドがメインスレッドかを返します

解説

現在のスレッドがメインスレッドかを返します。

返り値

( BOOL )

メインスレッドかYES/NO

引数

クラス

NSThread

Class Methods

使用可能

10.5

参照

+ mainThread

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
[NSThread detachNewThreadSelector:@selector(threadTask:) toTarget:self withObject: barA ];
[NSThread detachNewThreadSelector:@selector(threadTask:) toTarget:self withObject: barB ];

NSLog(@"%@------- ",(([[NSThread currentThread] isMainThread])?@"main":@"sub"));

if ([NSThread isMultiThreaded]){
    NSLog(@"YES");
}else{
    NSLog(@"NO");
}

}
- (void)threadTask:(id)obj
{
    int i;
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

  for (i=1;i<20000;i++) {
		
    NSLog(@"%@------- %d",(([[NSThread currentThread] isMainThread])?@"main":@"sub"),i);
	[obj setDoubleValue:(double)i];
    }

    [pool release];
    [NSThread exit];
}
@end