IOS dispatch_once
来源:互联网 发布:51单片机app 编辑:程序博客网 时间:2024/05/19 05:34
dispatch_once
Executes a block object once and only once for the lifetime of an application.
void dispatch_once( dispatch_once_t *predicate, dispatch_block_t block);
Parameters
- predicate
A pointer to a
dispatch_once_t
structure that is used to test whether the block has completed or not.- block
The block object to execute once.
Discussion
This function is useful for initialization of global data (singletons) in an application. Always call this function before using or testing any variables that are initialized by the block.
If called simultaneously from multiple threads, this function waits synchronously until the block has completed.
The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage is undefined.
dispatch_once不仅意味着代码仅会被运行一次,而且还是线程安全的,这就意味着你不需要使用诸如@synchronized之类的来防止使用多个线程或者队列时不同步的问题。
如果你要共享某个实例
+(MyInstance*)shareMyInstance()
{
static MyInstance *inst=nil;
static dispatch_once_t p;
dispatch_once(&p,^{
inst=[[MyInstance alloc]init];
});
return inst;
}
你任何时候访问共享实例,需要做的仅是:
MyInstance *myInst = [MyInstance shareMyInstance];
- IOS dispatch_once
- IOS dispatch_once
- iOS dispatch_once详解
- IOS 用dispatch_once创建单利
- IOS多线程之dispatch_once剖析
- iOS-GCD多线程之dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- IOS开发之dispatch_once创建单例
- 【ios】利用dispatch_once创建单例
- iOS系统GCD学习(7):dispatch_once
- iOS系统GCD学习(7):dispatch_once
- IOS开发之dispatch_once创建单例
- IOS--dispatch_once 创建单例singletons
- Web企业应用软件知识点整理
- Menu的窗口句柄
- JSP连接数据库方法大全
- Install linux from HDD/U-Disk into HDD/U-Disk
- How to get the useful synonyms
- IOS dispatch_once
- Tomcat 6.0 设置 JNDI 数据源
- Sql Server 2005之后新增的With AS 语法,更容易实现递归
- 功能最完善的uboot
- java自定义注解
- 使用properties配置spring
- 配置Linux—LVS (DR)
- 内核初始化优化宏 ,初始化顺序, __init,__devexit等
- Windows下配置python环境变量