ios多核心 处理代码

8 篇文章 0 订阅
// 多核心加速处理函数
// 在阻塞期间,创建和处理器核心数相同数量的线程来执行回调函数
// 例如 双核 会执行下面的方法
// pfun(0, 2)
// pfun(1, 2)
inline
void MultiCoreProcessing(void (^pfun)(int i_thread, int num_thread))
{
    NSUInteger processorCount = [[NSProcessInfo processInfo] processorCount];
    
    //单核CPU直接执行
    if (!OPT_MULTICORE || processorCount == 1)
    {
        pfun(0, 1);
        return;
    }
    
    unsigned short int count = processorCount;
    
    //信号量
    dispatch_semaphore_t *semPart = (dispatch_semaphore_t*)malloc(sizeof(dispatch_semaphore_t)*count);
    
    //启动
    for (int i=0; i<count; i++)
    {
        //创建一个等待的信号量
        semPart[i] = dispatch_semaphore_create(0);
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
            
            //执行回调方法
            pfun(i, count);
            
            dispatch_semaphore_signal(semPart[i]);
        });
    }
    
    //等待任务结束
    for (int i=0; i<count; i++)
    {
        dispatch_semaphore_wait(semPart[i], DISPATCH_TIME_FOREVER);
        dispatch_release(semPart[i]);
    }
    
    free(semPart);
}


用法:

void (^pfun)(int, int)  = ^void (int i_thread, int num_thread)
    {
        //将图片数据分割成 num_thread 次执行,每个线程处理一部分数据
        TiBitmapData *bitmapPart = SplitBitmap(bitmap, i_thread, num_thread);
        
        AdjustVibrance(*bitmapPart, saturation, vibrance);
        
        delete bitmapPart;
    };
    
    MultiCoreProcessing(pfun);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值