NSThread
导入头文件
#import <pthread.h>
pthread演练
- (void)pthreadDemo {
pthread_t threadId = NULL;
NSString *str = @"Hello Pthread";
int result = pthread_create(&threadId, NULL, demo, (__bridge void *)(str));
if (result == 0) {
NSLog(@"创建线程 OK");
} else {
NSLog(@"创建线程失败 %d", result);
}
}
void *demo(void *params) {
NSString *str = (__bridge NSString *)(params);
NSLog(@"%@ - %@", [NSThread currentThread], str);
return NULL;
}
小结
- 在 C 语言中,没有
对象
的概念,对象是以结构体
的方式来实现的
- 通常,在 C 语言框架中,对象类型以
_t/Ref
结尾,而且声明时不需要使用 *
- C 语言中的
void *
和 OC 中的 id
是等价的
- 在混合开发时,如果在
C
和 OC
之间传递数据,需要使用 __bridge
进行桥接,桥接
的目的就是为了告诉编译器如何管理内存
- 桥接的添加可以借助 Xcode 的辅助功能添加