C ++ multi-thread programming Summary

Source: Internet
Author: User

When developing C ++ programs, there are high requirements on throughput, concurrency, and real-time performance. When designing a C ++ program, we can summarize the following points to improve efficiency:
L concurrency
L asynchronous
L Cache
Below are some examples of problems I encountered in my daily work. The design concept is nothing more than the above three points.
1. task queue
1.1 design task queue with producer-consumer model
The producer-consumer model is a very familiar model. For example, in a server program, when the User data is modified by the logic module, a database update task (produce) is generated ), it is delivered to the task queue of the IO module. The IO module extracts the task from the task queue and performs the SQL operation (consume ).
Design a common task queue. The sample code is as follows:
For detailed implementation, see:
Http://ffown.googlecode.com/svn/trunk/fflib/include/detail/task_queue_impl.h
Void task_queue_t: produce (const task_t & task _){
Lock_guard_t lock (m_mutex );
If (m_tasklist-> empty ()){//! Wake up waiting thread when conditions are met
M_cond.signal ();
}
M_tasklist-> push_back (task _);
}
Int task_queue_t: comsume (task_t & task _){
Lock_guard_t lock (m_mutex );
While (m_tasklist-> empty ())//! When no job exists, wait until the conditions are met and wake up {
If (false = m_flag ){
Return-1;
}
M_cond.wait ();
}
Task _ = m_tasklist-> front ();
M_tasklist-> pop_front ();
Return 0;
}
1.2 task queue usage skills
1.2.1 IO and logic Separation
For example, in an online game server program, the Network Module receives a message packet and returns the packet immediately after it is delivered to the logic layer, continuing to accept the next message packet. The logical thread runs in an environment without io operations to ensure real-time performance. Example:
Void handle_xx_msg (long uid, const xx_msg_t & msg ){
Logic_task_queue-> post (boost: bind (& servie_t: proces, uid, msg ));
}
Note: In this mode, the queue is a single task, and each task queue is a single thread.
1.2.2 parallel pipeline
The above only completes the parallel io and cpu operations, while the logical operations in the cpu are sequential. In some cases, the cpu logic operation part can also be parallel. For example, in the game, user A and B can be parallel, because the two operations do not share data. The simplest way is to assign operations related to A and B to different task queues. Example:
Void handle_xx_msg (long uid, const xx_msg_t & msg ){
Logic_task_queue_array [uid % sizeof (logic_task_queue_array)]-> post (
Boost: bind (& servie_t: proces, uid, msg ));
}
Note: In this mode, a single thread is added to a multi-task queue.
1.2.3 connection pool and asynchronous callback
For example, the logic Service module requires the database module to asynchronously load user data and perform subsequent processing and computing. The database module has a connection pool with a fixed number of connections. When the SQL Execution task arrives, select an idle connection, execute the SQL statement, and pass the SQL statement to the logic layer through the callback function. The procedure is as follows:
N pre-allocated thread pool, each thread creates a connection to the database
N creates a task queue for the database module. All threads are consumers of this task queue.
N the logic layer wants the database module to deliver SQL Execution tasks and pass a callback function to accept SQL Execution results.
Example:
1 void db_t: load (long uid _, boost: functionpost (boost: bind (& db_t: load, uid, func ));
Note: This mode is a single task queue, with multiple threads in each task queue.
2. Logs
This article focuses on C ++ multi-threaded programming. The log system is not designed to improve program efficiency, but the log is an irreplaceable tool for debugging and running errors, I believe that all the friends who develop background programs will use logs. Common log usage methods are as follows:
N stream, such as logstream <"start servie time [% d]" <time (0) <"app name [% s]" <app_string.c_str () <endl;
N Printf format: logtrace (LOG_MODULE, "start servie time [% d] app name [% s]", time (0), app_string.c_str ());
The two have their own advantages and disadvantages. streaming is thread-safe, and the formatting string in printf format is more direct, but the disadvantage is that the thread is not secure. If you replace app_string.c_str () with app_string (std: string ), the compilation is successful, but it will crash during runtime (if you are lucky, crash may occasionally occur if you are lucky ). I personally love the printf style and can make the following improvements:
L increase thread security. The traits mechanism of the c ++ template can be used to implement thread security. Example:
Template
Void logtrace (const char * module, const char * fmt, ARG1 arg1 ){
Boost: format s (fmt );
F % arg1;
}
In this way, except for the standard type + std: string, other types cannot be compiled. Here we only list an example of a parameter. You can reload this version to support more parameters. If you want to, you can support nine or more parameters.
L add a color to the log. Add a control character to printf to display the color on the screen terminal. For Linux: printf ("\ 033 [32; 49; 1 m [DONE] \ 033 [39; 49; 0 m ")
L when each thread starts, logs should be used to print the functions that the thread is responsible. In this way, when the program runs, the top-H-p pid can be used to know the cpu usage of that function. In fact, every line of my log prints the thread id. This thread id is not pthread_id, but is actually the process id assigned by the system corresponding to the thread.
3. Performance monitoring
Although many tools are available to analyze the program running performance of c ++, most of them are still running in the program debug stage. We need a means to monitor the program in both the debug and release phases. On the one hand, we need to know the bottleneck of the program, and on the other hand, we need to find out which components encountered exceptions during the runtime as soon as possible.
Gettimeofday is usually used to calculate the overhead of a function, which can be precise to the subtle. You can use the deterministic structure of C ++ to easily obtain the function overhead. The example is as follows:
Struct profiler {
Profiler (const char * func_name ){
Gettimeofday (& TV, NULL );
}
~ Profiler (){
Struct timeval tv2;
Gettimeofday (& tv2, NULL );
Long cost = (TV. TV _sec-TV. TV _sec) * 1000000 + (TV. TV _usec-TV. TV _usec );
//! Post to some manager
}
Struct timeval TV;
};
# Define PROFILER () profiler (_ FUNCTION __)
Cost should be delivered to the Performance Statistics Manager, which regularly outputs Performance Statistics to files.
4. Lambda Programming
Use foreach instead of iterator
Many programming languages have built-in foreach, but c ++ does not. Therefore, you are advised to write the foreach function where you need to traverse the container. People who are used to functional programming should be very fond of using foreach. The advantages of using foreach are more or less, but they are mainly at the upper level of programming philosophy.
Example:
Void user_mgr_t: foreach (boost: function func _){
For (iterator it = m_users.begin (); it! = M_users.end () ++ it ){
Func _ (it-> second );
}
}
For example, to implement the dump interface, you do not need to rewrite the code about the iterator.
Void user_mgr_t: dump (){
Struct lambda {
Static void print (user_t & user ){
//! Print (tostring (user );
}
};
This-> foreach (lambda: print );
}
In fact, the code above is flexible to generate an anonymous function. If it is a c ++ 11 standard compiler, it can be written more concisely:
This-> foreach ([] (user_t & user ){});
However, most of the programs I write will run on centos. Do you know that its gcc version is gcc 4.1.2, so most of the time I use lambda functions in a flexible way.
Use Lambda functions with task queues for Asynchronization
The common code for implementing asynchronous tasks using task queues is as follows:
Void service_t: async_update_user (long uid ){
Task_queue-> post (boost: bind (& service_t: sync_update_user_impl, this, uid ));
}
Void service_t: sync_update_user_impl (long uid ){
User_t & user = get_user (uid );
User. update ()
}
The disadvantage of doing so is that an interface needs to write the function twice in response. If the parameter of a function changes, the other parameter also needs to be modified. And the code is not very beautiful. Lambda makes Asynchronization look more intuitive, as if it was done immediately in interface functions. Sample Code:
Void service_t: async_update_user (long uid ){
Struct lambda {
Static void update_user_impl (service_t * servie, long uid ){
User_t & user = servie-> get_user (uid );
User. update ();
}
};
Task_queue-> post (boost: bind (& lambda: update_user_impl, this, uid ));
}
In this way, when you want to modify this interface, you can directly modify the code in this interface, which is very intuitive.
5. skillful
Map/reduce using shared_ptr
The semantics of Map/reduce is to divide the task into multiple tasks and deliver them to multiple workers for concurrent execution. The resulting results are summarized by reduce to generate the final result. What is the meaning of Shared_ptr? When the last shared_ptr destructor is created, the managed object's destructor are called. The semantics is very similar to the map/reduce process. We only need to divide the requests into multiple tasks by ourselves. The sample process is as follows:
L define the request hosting object, add the number of times that the "oh nice" string appears in 10 files, and define the managed struct as follows:
Struct CER {
Void set_result (int index, long result ){
M_result [index] = result;
}
~ Reducer (){
Long total = 0;
For (int I = 0; I <sizeof (m_result); ++ I ){
Total + = m_result [I];
}
//! Post total to somewhere
}
Long m_result [10];
};
L define the worker that executes the task
Void worker_t: exe (int index _, shared_ptr ret ){
Ret-& gt; set_result (index, 100 );
}
L after dividing tasks, deliver them to different workers
Shared_ptr ret (new reducer ());
For (int I = 0; I <10; ++ I) {task_queue [I]-> post (boost: bind (& worker_t: exe, I, ret ));
}



From zhiran

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.