iOS中的多线程-GCD用法

瑞芯微RK3506开发板Ethercat主站性能优化实战:从内核配置到微秒级延迟控制 本文详细介绍了在瑞芯微RK3506开发板上实现EtherCAT主站微秒级延迟控制的实战优化方案。核心在于部署Preempt-RT实时内核,并进行深度系统调优,包括CPU隔离、中断亲和性设置、内存锁定及电源管理调整。通过绕过Linux网络栈的专用驱动加载与配置,最终实现了稳定的高性能实时控制,满足工业自动化需求。 阅读详情

GCD中涉及的基本名词:

线程:程序执行任务的最小调度单位

任务:需要在主线程或者子线程中执行的代码,在GCD中显示为Block中

队列:一种特殊的线性表,采取FIFO(先进先出),可以看做是用来存储任务的数组

异步:开辟新线程,同时执行多个任务

同步:在单一线程中执行,只能按顺序从前往后执行

并行队列:队列中的任务同时进行,可以开启多个线程,并发功能只有在异步情况有效

串行队列:队列中的任务逐个进行,只有单一线程

 

GCD的创建:

概述的讲,就是创建一个队列,然后将任务添加到队列中去

(1)创建队列

// ?????????串行队列
dispatch_queue_t queue = dispatch_queue_create("com.app", DISPATCH_QUEUE_SERIAL);
// ?????????并发队列
dispatch_queue_t queue = dispatch_queue_create("com.app", DISPATCH_QUEUE_CONCURRENT);

第一个参数是队列的标志符,可以为空,第二个参数表示是串行队列还是并发队列

GCD中有简便的方法获取队列,一个是获取主队列(串行队列),还有一个是获取全局并发队列(并发队列)

//主队列
dispatch_queue_t queue = dispatch_get_main_queue();
//全局并发队列,第一个参数表示优先级,第二个参数是保留参数,暂时无用
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DE FAULT, 0);

(2)创建任务

dispatch_sync(queue, ^{ 
    // 同步
});
???????dispatch_async(queue, ^{
    // 异步
});

 

GCD的使用:

- (void)gcdTest {
    NSLog(@"currentThread---%@",[NSThread currentThread]);
    NSLog(@"---begin");
    dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        //任务一
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@",[NSThread currentThread]);
        } });
    dispatch_sync(queue, ^{
        //任务二
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@",[NSThread currentThread]);
        } });
    dispatch_sync(queue, ^{
        //任务三
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@",[NSThread currentThread]);
        } });
    NSLog(@"---end");
}

(1)同步+并发,同步+串行

2019-02-15 20:30:13.134436+0800 GCD[11324:226430] currentThread---<NSThread: 0x6000019dafc0>{number = 1, name = main}
2019-02-15 20:30:13.134613+0800 GCD[11324:226430]---begin
2019-02-15 20:30:15.135160+0800 GCD[11324:226430] 1---<NSThread: 0x6000019dafc0>{number = 1, name = main}
2019-02-15 20:30:17.136650+0800 GCD[11324:226430] 1---<NSThread: 0x6000019dafc0>{number = 1, name = main}
2019-02-15 20:30:19.138189+0800 GCD[11324:226430] 2---<NSThread: 0x6000019dafc0>{number = 1, name = main}
2019-02-15 20:30:21.139706+0800 GCD[11324:226430] 2---<NSThread: 0x6000019dafc0>{number = 1, name = main}
2019-02-15 20:30:23.141219+0800 GCD[11324:226430] 3---<NSThread: 0x6000019dafc0>{number = 1, name = main}
2019-02-15 20:30:25.142714+0800 GCD[11324:226430] 3---<NSThread: 0x6000019dafc0>{number = 1, name = main}
2019-02-15 20:30:25.142980+0800 GCD[11324:226430]---end

这两种结果都一样,虽然并发队列有执行多线程的能力,但是由于是同步执行,只会在单一线程中执行,所以会在同一线程中依次执行完任务后再添加下一个任务

(3)异步+串行

2019-02-15 20:42:52.282373+0800 GCD[11506:241315] currentThread---<NSThread: 0x600003b93640>{number = 1, name = main}
2019-02-15 20:42:52.282539+0800 GCD[11506:241315] ---begin
2019-02-15 20:42:52.282674+0800 GCD[11506:241315] ---end
2019-02-15 20:42:54.286775+0800 GCD[11506:241365] 1---<NSThread: 0x600003bf1f00>{number = 3, name = (null)}
2019-02-15 20:42:56.287246+0800 GCD[11506:241365] 1---<NSThread: 0x600003bf1f00>{number = 3, name = (null)}
2019-02-15 20:42:58.292730+0800 GCD[11506:241365] 2---<NSThread: 0x600003bf1f00>{number = 3, name = (null)}
2019-02-15 20:43:00.293612+0800 GCD[11506:241365] 2---<NSThread: 0x600003bf1f00>{number = 3, name = (null)}
2019-02-15 20:43:02.299173+0800 GCD[11506:241365] 3---<NSThread: 0x600003bf1f00>{number = 3, name = (null)}
2019-02-15 20:43:04.300262+0800 GCD[11506:241365] 3---<NSThread: 0x600003bf1f00>{number = 3, name = (null)}

当遇到异步执行时,会创建一个新的线程,主线程继续向下执行,新线程执行添加的任务,新线程是串行队列,也是按顺执行

(4)异步+并行

2019-02-15 20:47:46.005210+0800 GCD[11582:247831] currentThread---<NSThread: 0x600000071400>{number = 1, name = main}
2019-02-15 20:47:46.005361+0800 GCD[11582:247831] ---begin
2019-02-15 20:47:46.005466+0800 GCD[11582:247831] ---end
2019-02-15 20:47:48.005804+0800 GCD[11582:247876] 2---<NSThread: 0x600000012940>{number = 4, name = (null)}
2019-02-15 20:47:48.005805+0800 GCD[11582:247877] 1---<NSThread: 0x60000001c0c0>{number = 3, name = (null)}
2019-02-15 20:47:48.005804+0800 GCD[11582:247879] 3---<NSThread: 0x60000001c100>{number = 5, name = (null)}
2019-02-15 20:47:50.008365+0800 GCD[11582:247876] 2---<NSThread: 0x600000012940>{number = 4, name = (null)}
2019-02-15 20:47:50.008365+0800 GCD[11582:247879] 3---<NSThread: 0x60000001c100>{number = 5, name = (null)}
2019-02-15 20:47:50.008387+0800 GCD[11582:247877] 1---<NSThread: 0x60000001c0c0>{number = 3, name = (null)}

这种情况就是主线程在执行,同时其他开辟多个线程同时执行

(5)同步+主队列

这种情况下,如果调用的函数在其他线程执行,和同步+串行情况相同,但是在主线程下执行,打印完begin之后会死锁,程序崩溃。因为函数在主队列,他会执行完函数再调用添加进来的任务,但是任务不执行完就没法走完函数,有点像循环引用,A引用B,B引用A,双方都等对方执行完再执行,这种情况要避免。

GCD的其他方法

(1)栅栏方法:?????dispatch_barrier_async

NSLog(@"currentThread---%@",[NSThread currentThread]);
    NSLog(@"---begin");
    dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        //任务一
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@",[NSThread currentThread]);
        } });
    dispatch_async(queue, ^{
        //任务二
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@",[NSThread currentThread]); 
        } });
    dispatch_barrier_async(queue, ^{
        NSLog(@"barrier---%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        //任务三
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@",[NSThread currentThread]);
        } });
    NSLog(@"---end");

结果

2019-02-16 00:02:50.327233+0800 GCD[13192:337727] currentThread---<NSThread: 0x6000010e2900>{number = 1, name = main}
2019-02-16 00:02:50.327496+0800 GCD[13192:337727] ---begin
2019-02-16 00:02:50.327658+0800 GCD[13192:337727] ---end
2019-02-16 00:02:52.333127+0800 GCD[13192:337760] 1---<NSThread: 0x600001087480>{number = 3, name = (null)}
2019-02-16 00:02:52.333147+0800 GCD[13192:337762] 2---<NSThread: 0x600001087500>{number = 4, name = (null)}
2019-02-16 00:02:54.335308+0800 GCD[13192:337760] 1---<NSThread: 0x600001087480>{number = 3, name = (null)}
2019-02-16 00:02:54.335321+0800 GCD[13192:337762] 2---<NSThread: 0x600001087500>{number = 4, name = (null)}
2019-02-16 00:02:54.335729+0800 GCD[13192:337762] barrier---<NSThread: 0x600001087500>{number = 4, name = (null)}
2019-02-16 00:02:56.336141+0800 GCD[13192:337762] 3---<NSThread: 0x600001087500>{number = 4, name = (null)}
2019-02-16 00:02:58.340345+0800 GCD[13192:337762] 3---<NSThread: 0x600001087500>{number = 4, name = (null)}

可以看出,栅栏函数能隔开加载到队列里的任务,未加载到队列里的任务无效。在栅栏函数前面的任务全部执行完后,执行栅栏函数,然后执行栅栏函数后的任务。

(2)延时执行方法:dispatch_after

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"after---%@",[NSThread currentThread]);
    });

顾名思义,就是延迟执行,上面代码是延迟3s,注意,这个延迟是3s后把任务加入队列,不是3s后就执行,如果你加入的是串行队列,3s后加入队列,但是前面的任务需要10s才执行完,那你得到结果的时间是13s。

(3)只执行一次:dispatch_once

static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
       //任务  
});

该函数能保证代码在程序运行过程中只执行一次,并且即使在多线程的环境下,也能保证里面的代码线程安全。

(4)快速迭代方法:dispatch_apply

    NSLog(@"---begin");
    dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT);
    dispatch_apply(10, queue, ^(size_t index) {
        NSLog(@"%zd---%@",index, [NSThread currentThread]);
    });
    NSLog(@"---end");
2019-02-16 00:32:36.142181+0800 GCD[13675:371444] ---begin
2019-02-16 00:32:36.142501+0800 GCD[13675:371494] 2---<NSThread: 0x6000019e0b00>{number = 4, name = (null)}
2019-02-16 00:32:36.142526+0800 GCD[13675:371495] 1---<NSThread: 0x6000019dfec0>{number = 3, name = (null)}
2019-02-16 00:32:36.142529+0800 GCD[13675:371444] 0---<NSThread: 0x600001985400>{number = 1, name = main}
2019-02-16 00:32:36.142537+0800 GCD[13675:371493] 3---<NSThread: 0x6000019e0a80>{number = 5, name = (null)}
2019-02-16 00:32:36.142644+0800 GCD[13675:371494] 4---<NSThread: 0x6000019e0b00>{number = 4, name = (null)}
2019-02-16 00:32:36.142678+0800 GCD[13675:371495] 5---<NSThread: 0x6000019dfec0>{number = 3, name = (null)}
2019-02-16 00:32:36.142693+0800 GCD[13675:371444] 6---<NSThread: 0x600001985400>{number = 1, name = main}
2019-02-16 00:32:36.142735+0800 GCD[13675:371493] 7---<NSThread: 0x6000019e0a80>{number = 5, name = (null)}
2019-02-16 00:32:36.142786+0800 GCD[13675:371494] 8---<NSThread: 0x6000019e0b00>{number = 4, name = (null)}
2019-02-16 00:32:36.142807+0800 GCD[13675:371495] 9---<NSThread: 0x6000019dfec0>{number = 3, name = (null)}
2019-02-16 00:32:36.143608+0800 GCD[13675:371444] ---end

dispatch_apply有点像for循环,如果是串行队列,和for差不多,依次N次任务加入到队列中,顺序执行,如果是并行队列,则是并发执行任务,从结果中看出,这个函数会调用主线程,不知道会不会在某些情况下造成阻塞。这个函数本身是同步的,所以会执行完全部任务,再执行下面的任务。

(5)dispatch_group

1、group基础

NSLog(@"currentThread---%@",[NSThread currentThread]);
    NSLog(@"group---begin");
    dispatch_group_t group =  dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@",[NSThread currentThread]);
        };
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@",[NSThread currentThread]);
        };
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@",[NSThread currentThread]);
        };
        NSLog(@"group---end");
    });
2019-02-16 01:16:04.203743+0800 GCD[14254:412235] currentThread---<NSThread: 0x600001b9a900>{number = 1, name = main}
2019-02-16 01:16:04.203910+0800 GCD[14254:412235] group---begin
2019-02-16 01:16:06.205824+0800 GCD[14254:412362] 1---<NSThread: 0x600001bf5000>{number = 3, name = (null)}
2019-02-16 01:16:06.205845+0800 GCD[14254:412365] 2---<NSThread: 0x600001bf5040>{number = 4, name = (null)}
2019-02-16 01:16:08.211355+0800 GCD[14254:412362] 1---<NSThread: 0x600001bf5000>{number = 3, name = (null)}
2019-02-16 01:16:08.211356+0800 GCD[14254:412365] 2---<NSThread: 0x600001bf5040>{number = 4, name = (null)}
2019-02-16 01:16:10.212927+0800 GCD[14254:412235] 3---<NSThread: 0x600001b9a900>{number = 1, name = main}
2019-02-16 01:16:12.214396+0800 GCD[14254:412235] 3---<NSThread: 0x600001b9a900>{number = 1, name = main}
2019-02-16 01:16:12.214740+0800 GCD[14254:412235] group---end

任务先加入到队列,然后队列加入到队列组,当队列组中的任务执行完成后,会执行dispatch_group_notify函数任务

2、wait

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

阻塞当前的线程,等到指定group中的任务执行完毕后再继续执行。

3、group_leave,group_enter

dispatch_group_enter ?????????标志着一个任务追加到group?,相当于group中未执行完毕的任务数+1

dispatch_group_leave ?????????标志着一个任务离开了group?,相当于group中未执行完毕的任务数-1

当group中未完成数为0的时候,才会执行notify和wait函数,这里有点像是引用计数。

(6)信号量dispatch_semaphore

dispatch_semaphore_create?:创建一个信号量

dispatch_semaphore_signal?:发送一个信号量,信号量+1

dispatch_semaphore_wait?:当线程信号量为0时阻塞线程,直到有新的信号量添加,当通过这个方法时,信号量的值-1

应用:保证线程安全,为线程加锁,或者保持线程同步,将异步执行任务转换成同步

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),                 ^{
    [NSThread sleepForTimeInterval:2];
    NSLog(@"2---%@",[NSThread currentThread]);
    dispatch_semaphore_signal(semaphore);
});
NSLog(@"1---%@",[NSThread currentThread]);
dispatch_semaphore_wait(semaphore,time);//DISPATCH_TIME_FOREVER没有废弃时间
NSLog(@"3---%@",[NSThread currentThread]);
2019-02-16 15:35:58.372359+0800 GCD[17462:642975] 1---<NSThread: 0x600002a193c0>{number = 1, name = main}
2019-02-16 15:36:00.375787+0800 GCD[17462:643022] 2---<NSThread: 0x600002a718c0>{number = 3, name = (null)}
2019-02-16 15:36:00.376150+0800 GCD[17462:642975] 3---<NSThread: 0x600002a193c0>{number = 1, name = main}

创建一个信号量,由于线程任务是异步执行,所以开辟了一个新线程去执行,主线程继续执行下面的打印任务,当遇到dispatch_semaphore_wait时,信号量为0,阻塞主线程。新线程由于有耗时操作,2s后执行完任务,打印并且使信号量+1,这时主线程中信号量为1,所以通过了wait方法,继续执行下面的打印。这里说下dispatch_semaphore_wait的第二个参数,是指等待时间,如果超出这个时间,也会继续向下运行,但是这个信号量就已经废弃了,下面再遇到dispatch_semaphore_wait方法也不会起作用。

 

 

网表导入virtuoso后发现pg pin忘记connect_pg_net/globalNetConnect怎么办? 数模混合项目中经常需要一些需要到virtuoso去连接的线,比如IO上的pg和一些信号线,除了在pr工作中设置skip route之外还需要做好net的赋值,告诉工具虽然我没在物理上有连接,但是实际上应该连什么net,这部分提现在网表里。在Default Net Name中也填写net name,因为这个net需要做global操作,即在顶层定义这个net,所以需要在net name后加个!打开schematic,删除指定cell pin所接net的label。 阅读详情

相关推荐

嵌入式常用通信协议 #概述

嵌入式通信协议

AFK3060的博客 2125

iOS开发多线程篇—GCD的常见用法

iOS开发多线程篇—GCD的常见用法(简单介绍): // 后台执行: dispatch_async(dispatch_get_global_queue(0, 0), ^{ // something }); // 主线程执行: dispatch_async(dispatch_get_main_queue(), ^{ // something });

lf20105103039的专栏 523

FOFA奇技淫巧:从搜索语法到实战场景的深度挖掘

本文深入探讨了FOFA搜索语法在网络空间测绘中的高效应用,从基础语法到实战场景的深度挖掘。通过渗透测试、威胁狩猎、资产发现等实际案例,展示了如何利用FOFA精准定位目标系统、识别蜜罐、追踪威胁,并提供了合规使用指南和性能优化技巧,助力安全工作者提升效率。

weixin_32428571的博客 244

iOS多线程之GCD用法详解

在上一篇讲解了iOS开发的三种多线程实现方式的特点和用法,在这一篇主要讲解一下GCD的用法。 GCD(Grand Central Dispatch)又叫大中央调度,它对线程操作进行了封装,加入了很多新的特性,内部进行了效率优化,提供了简洁的C语言接口,使用更加简单高效,也是苹果公司推荐的方式。 特点: GCD 可用于多核的并行运算 GCD 会自动利用更多的 CPU 内核(比如双核、四核) GCD...

如鱼得水的专栏 917

【ZYNQ驱动双AD9361实战】(一)从官方参考设计到自定义移植

本文详细介绍了如何将ADI官方的单AD9361参考设计移植到ZYNQ平台以驱动双AD9361射频板卡。内容涵盖从环境搭建、官方工程架构解析,到硬件HDL工程修改(包括顶层设计、引脚分配及TCL脚本适配)以及Linux设备树软件配置的全流程实战指南,帮助开发者实现自定义双通道射频收发系统。

weixin_29170327的博客 371

基于VS2012 C#调用bartender自动打印条形码

GitHub:https://github.com/sanjaywu/BarcodePrintTool 一、在引用添加Interop.BarTender.dll 1、在资源解决方案的引用这里,右击选择“添加引用”: 2、按照如下步骤添加Interop.BarTender.dll引用: 3、添加完成 二、使用bartender工具设置打印模板 1、页面大小设置: 2、条码设置: 三、代码...

Sanjay_Wu的博客 5965

iOS多线程技术3 - GCD的详细用法

多线程的相关概念 工欲善其事,必先利其器,在进入 GCD 学习之前,我们有必要先了解一下多线程的一些概念。 进程:进程是程序在计算机上的一次执行活动。例如打开一个 app,就开启了一个进程,一个进程可以并发多个线程。 线程:线程就是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一个线程同一时间只能执行一个任务,平时我们所说的多任务并发实际上是多个线程并发去...

毛毛虫 1859

【photoshop CEP插件】 OCR文字识别

一个photoshop CEP插件OCR文字识别 APP_ID = 17594920 API_KEY = puMFv72xqCYV2jdcSlXgtFvm SECRET_KEY = v5XHtqKXEm0p6ehPZrXIXrftDvocyv1m /*********************************************/ 百度OCR 密钥由Greless@qq.com 提供, 用户可自行前往https://ai.baidu.com/docs#/Auth/top 申请。 通用文字识别..

Greless的后花园 6508

IOS多线程之GCD的一些常规用法

首先介绍一下什么sh: Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统。这建立在任务并行执行的线程池模式的基础上的。它首次发布在Mac OS X 10.6 ,iOS 4及以上也可用。 设计: GCD的工作原理是:让程序平行排队的特定任务,根据可用的处理资源,安排他们在任何可用的处理器核心上执行任务

laomaoshiba的专栏 855

STM32G474片内运放实战:从独立模式到PGA与ADC采样链路

在嵌入式控制系统中,运放是连接模拟信号与数字处理的关键环节。传统方案依赖外部分立运放搭建放大电路,存在成本高、PCB布局敏感、一致性差等痛点。STM32G474在MCU内部集成多路可编程运放(OPAMP),支持独立模式、跟随器、PGA以及比较器模式,配合内部ADC和DMA可构成完整模拟采集链路。通过片内互联,信号路径大幅缩短,抗干扰性和通道一致性显著提升,尤其适用于电机电流采样、数字电源等场景。本文以STM32G474为例,详解从独立模式到PGA增益配置,再到ADC与DMA联动调试的完整过程,并给出采样时间

weixin_29194255的博客 262

CVPR 2026 即插即用 | 注意力篇 | MSEF:超轻注意力,空间-通道双分支并行融合,既增强特征又抑制噪声!

Multinex提出了一种超轻量级微光图像增强框架,其核心创新是MSEF模块。MSEF采用双分支并行架构,结合空间局部特征(深度可分离卷积)和全局通道注意力(改进SE),通过Tanh激活实现特征增强与抑制。相比传统注意力模块,MSEF在几乎不增加计算量的情况下显著提升性能,特别适合轻量级网络和边缘设备部署。实验表明,该模块能有效提升PSNR约3dB,同时保持极低的参数量(0.7K-45K)。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 624

iOS 多线程编程<二、GCD基本用法>

一:在学习多线程之前首先区分几个容易混淆的概念 二:GCD的基本用法 // // ViewController.m // GCD // // Created by fe on 16/9/30. // Copyright © 2016年 fe. All rights reserved. // #import "ViewController.h" @interface View

ZhengYanFeng1989的博客 437

为分析表明卡诺电池技术(或热泵式储电技术)-是目前最佳的储能技术!!!-2021年

卡诺电池(Carnot Battery)是一种基于热力学循环的储能技术,其核心原理是通过热力循环将电能转化为热能存储,并在需要时将热能重新转换为电能输出。这种技术得名于热力学中的卡诺循环原理,由法国工程师卡诺于1824年提出。

qq_15963745的博客 662

iOS多线程系列之三:GCD用法大全

##一、GCD简介 GCD(Grand Central Dispatch) 伟大的中央调度系统,是苹果为多核并行运算提出的C语言并发技术框架。 GCD会自动利用更多的CPU内核; 会自动管理线程的生命周期(创建线程,调度任务,销毁线程等); 程序员只需要告诉 GCD 想要如何执行什么任务,不需要编写任何线程管理代码 一些专业术语 dispatch :派遣/调度 queue:队列 用来存放任务的先进先出(FIFO)的容器 sync:同步 只是在当前线程中执行任务,不具备开启新线程

Elevason 3531

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

iOS 开发多线程篇—GCD的常见用法

iOS开发多线程篇—GCD的常见用法 一、延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [selfperformSelector:@selector(run)withObject:nilafterDelay:2.0]; // 2秒后再调用self的run方法 (2)使用GCD函数 dispatch_after(dis...

diyong1866的博客 147

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

iOS开发多线程篇—GCD的常见用法(一)

iOS开发多线程篇—GCD的常见用法 一、延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; // 2秒后再调用self的run方法   (2)使用GCD函数 dispatc

为你超神 610

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

iOS开发多线程-GCD的常见用法

一、延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; // 2秒后再调用self的run方法 (2)使用GCD函数 dispatch_after(dispatch_time(DISPATCH_TIME_NOW,

程序猿冰晨的专栏 1150

iOS 多线程—GCD 基本用法

什么是进程? 最通俗的描述就是一个个pid,官方的说法:进程是程序在计算机上的一次执行活动。打开一个app 就开启了一个进程。可包含多个线程。 什么是线程? 独立执行的代码段,一个线程同一时间内只能执行一个任务,反之多线程并发就可以在同一时间执行多个任务。 同步和异步 一个同步函数只在完成了预定任务后才返回。会阻塞当前线程。异步时任务开启会立即返回,不...

weixin_34126215的博客 107

iOS开发多线程篇 — GCD的常见用法

一、延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; // 2秒后再调用self的run方法 (2)使用GCD函数 dispatch_after...

diyong1866的博客 100

IOS多线程学习六:GCD的其他用法

文章目录一.延时执行1. NSObject的方法[self performSelector]2.使用GCD函数,dispatch_after3.[NSTimer scheduledTimerWithTimeInterval]二. 一次性代码三.多次操作四.队列组 一.延时执行 1. NSObject的方法[self performSelector] 2秒后再调用self的run方法 [self ...

煮酒Shae 268

iOS多线程技术—GCD的用法

iOS多线程技术—GCD的用法 一、主队列介绍 主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行。 提示:如果把任务放到主队列中进行处理,那么不论处理函数是异步的还是同步的都不会开启新的线程。 获取主队列的方式: 1 // 2 // YYViewController.m 3 // 12-GCD的基本...

weixin_30527551的博客 94

ios多线程-GCD基本用法

ios中多线程有三种,NSTread, NSOperation,GCD 这篇就讲讲GCD的基本用法 平时比较多使用和看到的是: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //异步操作代码块 dispatch_async(dispatc

kevinszj的专栏 377
上一篇: iOS中的分类、类扩展、子类
下一篇: UITabelView的headerVIew和footerView自适应
孤独流浪
博客等级 码龄14年 2粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值