iOS asynchronous and multithreaded operations && applications in Sqlite3

Source: Internet
Author: User

1, database I/O operations (Async)

The database itself is stored on disk. Access and modify the database, that is, read and write to the disk, i.e. I/O operations.

The disk belongs to the computer hardware, has the DMA ability, does not need the CPU intervention, may realize the asynchronous operation.

I/O operations are typically time consuming, and SQLite uses asynchronous processing I/O operations.

When there are multiple transactions that operate on the database, there will also be multiple I/O operations.

The operating system puts I/O operations into an I/O queue reasonably. A one-time delivery of I/O operations in the queue to the disk system, processing multiple I/O in parallel for increased efficiency. Please see the link in detail, I did not particularly in-depth study.

2, asynchronous and multithreaded

Asynchronous and multi-threaded, all have the ability to implement, do not block the current thread, make the application more "smooth". iOS is the most common and does not block the main thread, making the UI performance smoother.

There is a difference between the two. Please see the link for details.

In short, asynchronous implementation is based on the support of computer hardware, and multi-threading, is the operating system, a section of logic code control.

3, thread safety and handling

When a piece of code or a variable is accessed and used concurrently by multiple threads. These code or variables may have unintended effects, such as data disturbances and data security issues, which are "non-secure."

For thread safety, it is necessary to have only one thread accessing the code or variable at the same time, using "mutex" in iOS to avoid this situation.

(Mutex, mutex, meaning from itself words: incompatible, mutually exclusive.) Mathematically, A and B are mutually exclusive, meaning A and B do not have any intersection. A and B cannot occur at the same time. )

"Mutex" is @synchronized (object a) {}, object A, which can only be accessed by one thread.

Therefore, if a "mutex" is used, it must be in the case of multiple threads while accessing a copy of the resource.

Mutex, which increases the overhead of the CPU. In iOS, atomic is adding a "mutex" to the setter method. Be sure to use "nonatomic" instead of the case.

Multithreading of 4,sqlite3 and Sqlite3

Sqlite3 is a lightweight database. The Sqlite3 also uses asynchronous I/O operations to read data. For sqlite3 asynchronous I/O operations, see the links for details.

The sqlite3 in iOS is thread-safe. iOS UI in the main thread, does not block the UI thread, ensures fluency, so the operation of SQLite needs to be migrated to child threads. This is the main driving force for sqlite3 using multithreading.

There is a very good article, specific click on the link.

    • (1), single thread: Disables all mutex locks, and errors occur when used concurrently. This is enabled when SQLite compiles with the sqlite_threadsafe=0 parameter, or when Sqlite3_config (Sqlite_config_singlethread) is called before initializing SQLite.
    • (2), Multithreading: As long as a database connection is not used by multiple threads at the same time is safe. In the source code is enabled Bcoremutex, disable Bfullmutex. is essentially disabling the lock on the database connection and prepared statement (prepared statement), so you cannot concurrently use the same database connection or prepared statement in multiple threads. It is enabled by default when SQLite compiles with the sqlite_threadsafe=2 parameter. If Sqlite_threadsafe is not 0, you can call Sqlite3_config (Sqlite_config_multithread) before initializing SQLITE, or when creating a database connection, set the Sqlite_open_ Nomutex flag.
    • (3), Serial: Enable all locks, including Bcoremutex and Bfullmutex. Because both the database connection and the prepared statement are locked, multi-threaded use of these objects is not concurrent and becomes serial. It is enabled by default when SQLite compiles with the sqlite_threadsafe=1 parameter. If Sqlite_threadsafe is not 0, you can call Sqlite3_config (sqlite_config_serialized) before initializing SQLITE, or when creating a database connection, set the Sqlite_open_ Fullmutex flag.

For SQLite in iOS, the default (2) configuration is used. Inconvenient, if you want to be accessed by another thread SQLite, the other must shut down the database connection. There are many inconvenient places in the code.

Configuration (1) is not considered as a single thread.

Configuration (3), the multi-threaded serial mode is executed,

(Pause: )

iOS asynchronous and multithreaded operations && applications in Sqlite3

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.