pthread--常用操作

1. 创建:

	
// callback fun
void* thread_run(void* param){
	
	while (YES) {
		// wait
		//pthread_cond_wait(&g_cond, &g_mutex);
		
		static int i = 0;
		NSLog(@"runing i = %d\n", i++);
		
		sleep(1);
	}
	NSLog(@"runing end");
	return NULL;
}
pthread_t            g_handle = 0;
pthread_attr_t        g_attr;

 int iRet = pthread_attr_init(&g_attr);//iRet = pthread_create_suspended_np(&g_handle, &g_attr, thread_run, self);
iRet = pthread_create(&g_handle, nil, thread_run, self);



2.  锁和信号量的创建

static pthread_mutex_t		g_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t		g_cond = PTHREAD_COND_INITIALIZER;

// start
  pthread_cond_signal(&g_cond);


3. 中断以及结束

// 如果是直接中断 正在执行的函数体,例如while循环
pthread_cancel(g_handle);

// 如果是结束线程
pthread_cancel(g_handle);

//pthread_detach(g_handle);
pthread_exit(nil);
g_handle = NULL;

但是在线程函数外使用
pthread_exit(nil); 时, 会使当前函数阻塞,进一步研究中。。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值