Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ iOS GCD multithreading simple to use     - Execute command sentence can result in equipment permanently bricked in Linux laptop (Linux)

- Java synchronization mechanism used in locking Thought (Programming)

- Linux iptables firewall settings (Linux)

- Red Hat Linux security settings document (Linux)

- To share Linux script automatically change passwords (Linux)

- MySQL partition table Comments (Database)

- Ubuntu U disk do not have write privileges can only read but not write (Linux)

- Python: Finding meet the conditions specified in the file directory (Programming)

- Ubuntu Linux use MAC binding against ARP attacks (Linux)

- shell script error dirname: invalid option - b (Database)

- Oracle Linux 6.4 (BOND) dual NIC teaming combat - Annotated (Linux)

- How to restart after a crash Cinnamon (Linux)

- 10 Linux in the passwd command examples (Linux)

- MySQL monitoring tools -orzdba (Database)

- About enhanced Linux / Unix server system security program (Linux)

- Ubuntu 12.04 installation OpenCV2.4.1 and compile test (Linux)

- Three details reflect the Unix system security (Linux)

- Linux (RHEL6 CENTOS6 OLE6) VNC-SERVER Installation and Configuration (Server)

- Jetty JNDI Development combat (Linux)

- CentOS 7.0 local address and configure yum source address priority (Linux)

 
         
  iOS GCD multithreading simple to use
     
  Add Date : 2018-11-21      
         
         
         
  In iOS developers, Apple offers three multi-threading technology, namely:

(1) NSThread

(2) NSOperation

(3) GCD

A brief introduction to the use of GCD.

GCD stands for Grand Central Dispatch, can be called Grand Central scheduling. GCD is actually manages a thread pool, how to create a thread, how to recycle threads, as well as the number of threads allocated, these are the GCD controlled. In development, the programmer is not operating threads related matter, the programmer should only need to do an operation to put the appropriate queue inside.


A: Custom queue

There are several GCD queue, where custom queues in two ways: serial and parallel queue queues

 1: Serial queue: queue task will only execute the order, and only once capable of performing a task. That is, after performing a task, will perform the next task.

2: Parallel Queue: You can perform more than one task. For example, there are 10 parallel queue task, perform three tasks at once, which perform these three tasks done, followed by the implementation of the remaining tasks.

Note: either serial or parallel queue queue, they are FIFO (First In First Out) is. That is, either the queue, the queue into the task sooner, the sooner its execution time (in some cases only the end time of task execution is uncertain).

GCD has two modes of operation, namely synchronous and asynchronous operations

1: Synchronous operation: do not open new thread

2: asynchronous operation: will open a new thread

Two operations and two kinds of queues, a combination of four cases, in fact, in the development, some combinations are basically not used. The following applications describe four combinations.

A combination of: a serial queue + sync operation (not the new thread, and the task is executed one by one, so in fact is the order of execution), as follows:

- (Void) gcdDemo1


{

    // Serial synchronous operation queue +

    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_SERIAL);

    for (int i = 0; i <10; ++ i) {

        dispatch_sync (queue, ^ {

            NSLog (@ "% @% d", [NSThread currentThread], i);

        });

    }

}

number = 1, the main thread is described, no new open thread.

A combination of two: + serial asynchronous operations queue (because the task to be executed one by one, but because it is an asynchronous operation, it will open a new thread, all tasks are performed on new thread), as follows:

- (Void) gcdDemo1
{
    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_SERIAL);
    // Serial asynchronous operations queue +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

number = 2, instructions to open a new child thread, but still the order of execution.


A combination of three: + sync queue parallel operation (as the synchronization operation does not open a new thread, so even begin parallel queue multiple tasks at once, but in fact still is that each task is executed on the main thread, and order execution ). Code is as follows:

- (Void) gcdDemo2
{
    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_CONCURRENT);
    // Parallel synchronization tasks queue +
    for (int i = 0; i <10; ++ i) {
        dispatch_sync (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Not open a new thread and executed sequentially.

A combination of four: + parallel queue asynchronous operations (parallel queue will start once more tasks, and asynchronous operation to open a new thread, so the same time may perform multiple tasks simultaneously open multiple threads and ending time for each task is uncertain). Code is as follows:

- (Void) gcdDemo2
{
    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_CONCURRENT);
    // Parallel asynchronous task queue +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

We can see, open multiple threads, and the task is not complete execution of the order.

Two: global queue

In order to facilitate the development, Apple has also provided a global queue, the queue is actually a parallel global queue, therefore, the implementation of the results and the results of parallel queues global queue is consistent. Code is as follows:

Global queue + sync tasks:

- (Void) gcdDemo3
{
    dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // Global synchronization task queue +
    for (int i = 0; i <10; ++ i) {
        // Synchronization task
        dispatch_sync (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Global queue + asynchronous tasks:

- (Void) gcdDemo3
{
    dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // Global queue asynchronous tasks +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Three: the main queue

Apple also provides a main queue is a queue, the queue is the main serial queue, but there are differences and serial queue. The main task queue should be executed sequentially on the main thread, there is no concept of asynchronous. In other words, even if the asynchronous task execution queue on the primary, it will not open a new thread.

+ Main queue asynchronous tasks:

- (Void) gcdDemo4
{
    dispatch_queue_t queue = dispatch_get_main_queue ();
    // Main queue asynchronous tasks +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

We can see, does not open a new thread and executed sequentially.

The main queue + sync tasks (threads will be blocked), as follows:

- (Void) gcdDemo4
{
    dispatch_queue_t queue = dispatch_get_main_queue ();
    // + Sync main task queue and can block
    for (int i = 0; i <10; ++ i) {
        dispatch_sync (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Blocking reasons:

Main queue itself is a task A (main task), and the task A has not yet finished execution. During the execution of task A, and insert a new synchronization task B. We know that the serial queue, you must first perform a task completed, in order to continue to perform another task. At this time when:

To continue to perform tasks A, we need to first task B executed, if you want to continue with the task B, task A need to first executed, thus creating a blockage.

In development, we should avoid such blocking.
     
         
         
         
  More:      
 
- MySQL 5.7.10 source code for the latest version of the installation process in detail (Database)
- Linux system using the command line shutdown or restart (Linux)
- Do not find ifconfig eth0 and IP address under CentOS6.5 (Linux)
- CentOS6 installed Tomcat (Server)
- Linux firewall settings instance (Linux)
- Use py2exe to generate exe files Python script (Programming)
- Kubernetes Cluster Setup problems encountered and solutions (Server)
- Linux Getting Started tutorial: hard disk partition and to deal with traps (Linux)
- PLSQL Developer synchronization table tools (Database)
- Linux Getting Started tutorial: Experience VirtualBox Virtual Machine chapter (Linux)
- Teach you how to synchronize Microsoft OneDrive in Linux (Linux)
- Linux, grep, sed usage (Linux)
- CentOS7 Minimal minimize installation and then install the GNOME graphical interface (Linux)
- Compression software on a simple comparison of zip and gz (Linux)
- Getting Started with Linux system to learn: How to compress JPEG images on the command line (Linux)
- Ubuntu Backup and Recovery (Linux)
- How to install Linux Go Language (Linux)
- Getting Started with Linux system to learn: how to install USB webcams come in raspberry (Linux)
- After VMware CentOS full VM clone the network card is unavailable Solutions (Linux)
- How to make GRub instead of the default Ubuntu software center (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.