当前位置:网站首页>GCD implementation and arc, blocks, GCD usage examples
GCD implementation and arc, blocks, GCD usage examples
2022-07-28 23:16:00 【Fluttering moth】
Dispatch Queue
Usually , The code for thread management written in the application should be implemented at the system level .
No matter how hard programmers try to write code to manage threads , In terms of performance, it is impossible to surpass XNU Implemented at the kernel level GCD.
Used to implement Dispatch Queue And the software components used :
- libdispatch Provide technology :Dispatch Queue;
- Libc(pthreads) Provide technology :pthread_workqueue;
- XNU kernel Provide technology :workqueue;
GCD Of API All are included in libdispatch In the library C Language functions .
Dispatch Queue Through the structure and linked list, it is realized as FIFO queue . The queue manager passed dispatch_async Function Block.
Block Join first Dispatch Continuation this dispatch_continuation_t Type structure , And then we're going to add FIFO In line . The Dispatch Continuation For memory Block Of Dispatch Group And other information , It is equivalent to the execution context .
dispatch_set_target_queue Function can be set Dispatch Queue For another Dispatch Queue The goal of . It can be like stringing beads , Set up multiple connected Dispatch Queue. At the end of the connection, it must be set to Main Dispatch Queue, Or various priorities Global Dispatch Queue Or to be used for Serial Dispatch Queue Of various priorities Global Dispatch Queue.
Main Dispatch Queue stay RunLoop In the implementation of Block.
Global Dispatch Queue Yes 8 Types .
ARC、Blocks、GCD Examples of use
Implement from the specified URL Download data , Parse the data in another thread and use its parsing results in the main thread :
NSString *url = @"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fphoto.16pic.com%2F00%2F54%2F75%2F16pic_5475997_b.jpg&refer=http%3A%2F%2Fphoto.16pic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617284495&t=3bacab64a5d8233a37c94e2cc7f47492";
// In the main thread , From the specified url Start asynchronous network download
NSURL *postUrl = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:postUrl];
request.HTTPMethod = @"GET";
[request setTimeoutInterval:15];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^
{
// It does not prevent the main thread from processing the acquired data for a long time
dispatch_async(dispatch_get_main_queue(), ^
{
// React to the interface
});
});
}];
[task resume];
[session finishTasksAndInvalidate];Cocoa The framework provides for asynchronous network communication API, Therefore, threads cannot be used in network programming . Be sure to use API.
We are based on NSURLSession Encapsulated asynchronous network request :
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^completeBlock_t) (NSData *data);
typedef void (^errorBlock_t) (NSError *error);
@interface ASyncURLSession : NSURLSession
{
/**
* because ARC It works , So the following variables that do not show additional ownership modifiers ,
* All attached __strong Modifier variables .
*/
NSMutableData *data;
completeBlock_t completeBlock_;
errorBlock_t errorBlock_;
}
+(id)request:(NSString *)requestURL
completeBlock:(completeBlock_t)completeBlock
errorBlock:(errorBlock_t)errorBlock;
-(id)initWithRequest:(NSString *)requestURL
completeBlock:(completeBlock_t)completeBlock
errorBlock:(errorBlock_t)errorBlock;
@end
NS_ASSUME_NONNULL_END
边栏推荐
- The tenth improvement of yolov5: the loss function is improved to Siou
- Improvement 14 of yolov5: replace the backbone network C3 with the lightweight network GhostNet
- Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?
- [MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)
- frontiers出版社投稿记录(附状态变化)
- WebView whitelist
- can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to host memory
- sql优化常用的几种方法
- Target detection notes -yolo
- Introduction to structure
猜你喜欢

No code development platform management background tutorial

Basic concept of MySQL database and deployment of MySQL version 8.0 (I)

CGLIb 创建代理

Yolov5 improvement 5: improve the feature fusion network panet to bifpn

Cnpm installation steps

Learning experience sharing 4: learning experience of yolov7

cnpm安装步骤

Performance optimized APK slimming

6 个超级良心的开源教程!
![[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code](/img/31/e4cd4c261a7fc5cfa731976314530b.png)
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
随机推荐
WebApplicationType#deduceFromClasspath
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
Written questions and answers of software test interview (software test question bank)
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
MySQL foundation - advanced functions
WebApplicationType#deduceFromClasspath
This year, MediaTek 5g chip shipments are expected to reach 50million sets!
终端输出g_debug()信息
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
Improvement 16 of yolov5: replace backbone network C3 with lightweight network pp-lcnet
投资500亿元!中芯京城正式注册成立!
Thesis reading (0) - alexnet of classification
[copy] Internet terms, abbreviations, abbreviations
Target detection notes SSD
Learning experience sharing 5: yolov5 dataset division and Yolo format conversion
mgr.exe病毒导致启动程序启动失败
Yolov5 improvement 5: improve the feature fusion network panet to bifpn
cannot resize variables that require grad
18张图,直观理解神经网络、流形和拓扑
今年联发科5G芯片出货有望达到5000万套!