ios多核心 处理代码

来源:互联网 发布:java 电商平台 编辑:程序博客网 时间:2024/04/30 03:43
// 多核心加速处理函数// 在阻塞期间,创建和处理器核心数相同数量的线程来执行回调函数// 例如 双核 会执行下面的方法// pfun(0, 2)// pfun(1, 2)inlinevoid 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);

原创粉丝点击