Synchronization of iOS Concurrency Programming Guide

Source: Internet
Author: User

1.gcd

Fmdb uses GCD, which is to invoke the calling method from multiple threads simultaneously by establishing a serialized g-c-d queue, and GCD executes in the order in which it receives the blocks.

Fmdb uses Dispatch_sync, multi-threaded call a serialized QUEUE,GCD will execute on the receiving block and block other threads .

Using Fmdatabasequeue and Thread safety

It is unwise to use one Fmdatabase instance in multiple threads at the same time. Now you can create a Fmdatabase object for each thread. Do not let multiple threads share the same instance, it cannot be used concurrently in multiple threads.  If this happens, the bad things often happen, the program crashes from time to time, or reports anomalies, or meteors fall from the sky and hit your Mac Pro. In short, it collapsed. Therefore, do not initialize the Fmdatabase object and then use it in multiple threads. Please use Fmdatabasequeue, it is your friend and will help you. Here's how to use it:

The fmdatabasequeue background creates a serialized g-c-d queue and executes the blocks you pass to the g-c-d queue. This means that you invoke the calling method from multiple threads at the same time, and GDC executes in the order of the blocks it receives. No one can quarrel to whose feet everyone is happy.

The examples in the concurrencyprogrammingguide are as follows:

dispatch_queue_t mycustomqueue;
Mycustomqueue = Dispatch_queue_create ("Com.example.MyCustomQueue", NULL);

dispatch_async (Mycustomqueue, ^{

printf ("Do some work here.\n");

printf ("the first block may or is not having run.\n") ;

dispatch_sync (Mycustomqueue, ^{

printf ("do some to work here.\n");

});
printf ("Both blocks has completed.\n");

This is very similar to a traditional thread. In "UNIX Network Programming Volume 2", a large number of synchronization methods are introduced.

Part 3. Synchronization
7. mutexes and Condition Variables
8. Read-write Locks
9. Record Locking
Ten. Posix semaphores
System V semaphores

If the thread using the dispatch_async call is not blocked. This is similar to the nsoperationqueue synchronization.

2.nsoperationqueue

nsoperationqueue   setmaxconcurrentoperationcount: method can be configured Operation Queue 1 Queue You can only perform one operation at a time.

nsoperationqueue and nsoperationqueue creates a thread. and execute it in this thread.

that is to say: Multithreadingcalledaddoperation:method to add aOperationto theQueue, all the threads will return immediately,nsoperationqueue When the thread finishes executing, the nsoperationqueue The thread called Block.

AlthoughNsoperationqueueClass is designed for concurrent executionOperations, you can also force a singleQueueYou can only perform oneOperation. Setmaxconcurrentoperationcount: method to configure the maximum number of concurrent operations for the Operation queue. Set to 1 to indicate that the queue can only perform one operation at a time. However , the order in which operation is executed still depends on other factors, such as whether the operation is ready and prioritized. Therefore, the serialized operation Queue is not equivalent to the serial dispatch queue in GCD.

Original: http://www.cnblogs.com/javastart/p/4248290.html

Synchronization of iOS Concurrency Programming Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.