zoukankan      html  css  js  c++  java
  • 关于信号量以及多线程的代码

    #define MAX_CONCURRENT_COUNT 8
    
    static dispatch_semaphore_t WXDisplayConcurrentSemaphore;
    
    @implementation WXDisplayQueue
    
    + (dispatch_queue_t)displayQueue
    {
        static dispatch_queue_t displayQueue = NULL;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            displayQueue = dispatch_queue_create("com.taobao.weex.displayQueue", DISPATCH_QUEUE_CONCURRENT);
            dispatch_set_target_queue(displayQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
        });
        
        return displayQueue;
    }
    
    + (void)addBlock:(void(^)())block
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSUInteger processorCount = [NSProcessInfo processInfo].activeProcessorCount;
            NSUInteger maxConcurrentCount = processorCount <= MAX_CONCURRENT_COUNT ? processorCount : MAX_CONCURRENT_COUNT;
            WXDisplayConcurrentSemaphore = dispatch_semaphore_create(maxConcurrentCount);
        });
        
        dispatch_async([self displayQueue], ^{
            dispatch_semaphore_wait(WXDisplayConcurrentSemaphore, DISPATCH_TIME_FOREVER);
            block();
            dispatch_semaphore_signal(WXDisplayConcurrentSemaphore);
        });
    }
    
    @end
    

      

  • 相关阅读:
    PowerDesigner11.0的SQL生成表,写列描述出错
    centos中crontab(计时器)用法详解
    5.14
    4.13
    5.15
    监听服务启动失败
    4.24
    Enjoy 4.26
    4.14
    export
  • 原文地址:https://www.cnblogs.com/wxm5558/p/semphore.html
Copyright © 2011-2022 走看看