Portability and cross-platform development of C ++ [6]: Multithreading

Source: Internet
Author: User
Tags wxwidgets

This series has not been updated for a long time because of the many posts written in the past month. As a result, some netizens urged me in the comments, and I was a bit confused. Today, I will try to add multiple threads. Last chat Operating System

Because the OS-related topics are trivial, there are a lot of things. At that time, the multi-process and multi-thread sections were left behind.

Compiler

◇ About C Runtime Library options

Let's start with a very basic question: how to set the C Runtime Library (CRT: C run-time. I didn't want to talk about such a low-level problem, but there are several people around me who have suffered losses in this place. So let's talk about it.

Most C ++ compilers have their own CRT (more than one ). Some built-in CRT compilers may be divided into single-thread CRT and multi-thread CRT Based on thread support. When you need multi-threaded development, make sure that the related C ++ project uses multi-threaded CRT. Otherwise it will become ugly.

Especially when you use visual c ++ to create a project, be more careful. If the newly created project does not contain MFC (including the console project and Win32 project), the default setting of the project uses "Single-thread CRT", as shown in:

◇ Optimization Options

"Optimization Options" are another critical compiler-related topic. Some compilers provide optimization options that are known as awesome, but some optimization options may have potential risks. The compiler may make its own claim to disrupt the execution order of commands, leading to unexpected thread race (Race Condition). For more information, see "here

"). Liu weipeng's "C ++ multi-threaded Memory Model"

Let's take a look at some typical examples.

We recommend that you only use the regular speed optimization options of the compiler. The effects of other fancy optimization options may not be obvious, but the potential risk is not small. It is not worth taking risks.

Using GCC as an example: we recommend that you use-O2

Option (actually-O2

Is a collection of options), there is no need to take risks-O3

(Unless you have good reasons ). Besides-O2

And-O3

In addition, GCC also has a large (probably hundreds) of other optimization options. If you attempt to use an option, you must first understand its features and possible side effects. Otherwise, you will not know how to die.

Thread library selection

Since the current C ++ 03 standard almost does not involve thread-related content (even if C ++ 0x contains the standard library of the thread in the future, compiler vendor support may not be comprehensive in a short period of time). Therefore, for a long time to come, cross-platform multi-threaded support still depends on third-party libraries. Therefore, the choice of the thread library is very important. The following describes several well-known cross-platform thread libraries.

◇ Ace

Let's talk about ACE, a database with a long history. If you have never touched it before, read "here

"Literacy. From the full name of ACE (Adaptive Communication Environment), it should be based on "communication. However, Ace's support for the "multi-thread" sub-industry is still comprehensive, such as the mutex lock (ace_mutex), the condition variable (ace_condition), the semaphore (ace_semaphore), and the barrier (ace_barrier), atomic operations (ace_atomic_op) and so on. For some types, such as ace_mutex, it is also subdivided into the thread read/write lock (ace_rw_thread_mutex) and the thread recursive lock (ace_recursive_thread_mutex.

In addition to comprehensive support, ACE also has another obvious advantage, that is, it supports various operating system platforms and their built-in compilers well. Including some older compilers (such as vc6), which can also supportSupported

, Not only can be compiled, but also must be able to run stably ). This advantage is quite obvious for cross-platform development.

What are the disadvantages? Since Ace started a long time ago (probably in the middle of the 1990s S), many old features of c ++ were not available yet (not to mention new features ), therefore, we feel that the overall style of ACE is relatively old, far less fashionable and avant-garde than boost.

◇ Boost: Thread

Boost: The thread exactly matches ace. This seems to be from boost
Version 1.32 was introduced and is shorter than ace in years. However, thanks to the support of a group of big cows in boost, the development is still quite fast. Boost
Version 1.38 also supports many features (but it does not seem that there are many Ace features ). Since many members of the C ++ Standards Committee are gathered in the boost community
Shift, boost: thread will eventually become the star of the C ++ thread tomorrow, with a promising future!

Boost: the disadvantage of thread is that there are not enough compilers, especially someVintage

Compiler (many boost sub-libraries have this problem, mostly because some advanced template syntax is used ). This is an obvious problem for cross-platform platforms.

◇ WxWidgets

And QT

WxWidgets and QT are both GUI Libraries, but they are also built-in and support for threads. For the wxWidgets thread introduction, see "here

", For introduction to QT threads, see" here

". These two libraries support almost all threads and provide common mechanisms such as mutex, condition, and semaphore. However, there are not many Ace features.

How to weigh

If you have used wxWidgets or QT to develop GUI software, you can directly use their built-in thread libraries (provided that you only use basic thread functions ). Because of their built-in thread library, the features are slightly thin. If you need an advanced thread function, replace it with boost: thread or ace.

As for Boost: the trade-off between thread and ACE mainly depends on the needs of the software. If you want to support a lot of platforms, we recommend that you use ACE to avoid problems that the compiler does not support. If you only need to support a few mainstream platforms (such as Windows, Linux, and Mac), we recommend using boost: thread. After all, the compilers in mainstream operating systems have excellent support for boost.

Programming considerations

In fact, there are a lot of things to pay attention to in multi-threaded development. I can only list a few important notes.

◇ AboutVolatile

When it comes to the traps that may be encountered by multithreaded programming, we have to mentionVolatile

Keyword. If you do not know much about it, read "here

"Literacy. Since neither the C ++ 98 nor C ++ 03 standards define a multi-threaded memory modelVolatile

Touch the thread. As a result, the C ++ community has a large number of ports concentrated inVolatile

Body (which has a lot of C ++ bits ). In view of this, I will not be too long here. I recommend some articles from Daniel Andrei Alexandrescu.

Article "here

"And the Hans Boehm article" here

"And" here

". Everyone should read it by themselves.

◇ About atomic operations

Some students only know multiple threads.Competitive write

Lock required, but do not know multipleRead

SingleWrite

Protection is also required. For example, an integer int ncount = 0x01020304; In a concurrent State, a write thread modifies its value ncount = 0x05060708; another read thread obtains the value. Is it possible for the read thread to read a "bad" data pinch (such as 0x05060304?

Whether the data is corrupted depends on whether the ncount read and write operations are atomic. This relies on many hardware-related factors (including the CPU type, CPU font length, and memory-aligned bytes ). In some cases, data may indeed break down.

Since we are talking about cross-platform development, Tian knows in the future what kind of hardware environment your code will be executed. Therefore, when dealing with similar problems, we should use the atomic operation class/function provided by a third-party library (such as the atomic_op of ACE) to ensure security.

◇ Object Structure Analysis

In the previous series of posts, "How did c ++ objects die?

", The threads under the Win32 and POSIX platforms are introduced respectively.Non

Natural Death problems. Since the underlying layers of the above cross-platform thread libraries still need to call the built-in thread APIs of the operating system, everyone should do their best to ensureAll

All threads can die naturally.

Today's topic is here to talk about the multi-process topic next time.


Copyright Notice

All original articles in this blog are copyrighted by the author. This statement must be reprinted to keep this article complete, and the author's programming will be noted in the form of hyperlinks
And the original address of this article:

Http://program-think.blogspot.com/2009/04/cxx-cross-platform-develop-6-thread.html

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.