Pthread Win32 multi-threaded programming knowledge and feelings, pthread multi-threaded programming

Source: Internet
Author: User

Pthread Win32 multi-threaded programming knowledge and feelings, pthread multi-threaded programming

One major criticism of studying genetic algorithms is that the results of each program run are not exactly the same. Sometimes the optimal solution can be found and sometimes the optimal solution cannot be found, which is caused by the probability of genetic algorithms. So how to evaluate the quality of your method? At this time, it is necessary to run the program multiple times to obtain the average value of the result or calculate the running success rate of the algorithm. The problem is that the running time of the genetic algorithm is a little longer, especially when the number of tested datasets is large and large, it is sometimes unacceptable to run an experiment to finish all the data. So I thought of using multithreading, so that I could run multiple groups of independent experiments at the same time, and finally integrate the output data of all sub-threads to calculate the running success rate or take the average value of the optimal solution, with this idea in mind, I began to try multi-threaded programming to speed up the experiment. The following are the learning and experiment processes and results of these days.

I have been using VS 2010 programming in Windows, after reading a lot of blogs and materials about multithreaded programming, I decided not to use win32 APIs to implement multithreading (the names of various functions are too long to look ), the use of pthread windows Development Kit (http://sourceware.org/pthreads-win32/ download arbitrary, this download is the latest version of pthreads-w32-2-9-1-release.zip, in VS configuration method described later) more convenient. So, multi-thread programming is coming soon ~~~

Step 1: Configure pthread in VS2010
Download pthread and decompress it to any folder. decompress it to include three folders: Pre-built.2 pthreads.2 and QueueUserAPCEx. Only the first folder is used in configuration. Opening the Pre-built.2 folder opens to the three folders dll, include, lib, and a pair of other files, from which you can see how to configure.

1. Add the include path to the include path of. For example, my include path is F: \ C & C ++ \ multithreading programming \ pthreads-w32-2-9-1-release \ Pre-built.2 \ include. Add this path to the include path

Choose Property Manager> right-click Properties> VC ++ directory> include directory, copy and paste the directory to be added and click "OK" (do not add the "$" symbol before the path, otherwise an error will occur)

 

2. Enter the library directory. In the same step, add the directory of the lib folder to the library directory.

3. Add the lib file to the additional dependency. Choose Property Manager> right-click Properties> linker> input> Add dependency. Copy and paste the. lib file name in the lib folder under the project. Remember to wrap each lib file name.

4. copy and paste the dll file in the dll folder to the root directory of your VS project. You can start to use pthread To Write multi-threaded programs in.

Step 2. Programming

Below is my program

# Include "rcpsp. h "// self-compiled header file # include <pthread. h> // The pthread header file, multi-threaded things are in it // ================ 10 threads ====================== ==========/// each thread is an independent experiment, each group of data runs independently for ten times, so we need to write ten sub-threads to quickly complete the void * process_0 (void * arg); void * process_1 (void * arg ); void * process_2 (void * arg); void * process_3 (void * arg); void * process_4 (void * arg); void * process_5 (void * arg ); void * process_6 (void * arg); void * process_7 (void * arg); void * process_8 (void * arg); void * process_9 (void * arg ); // ================================================ ===== typedef struct {// ============= Input Variables ================= .... // ============= Output Variables ============== ....} inputData; int main (void) {pthread_t p0, p1, p2, p3, p4, p5, p6, p7, p8, p9; // defines the pthread variable InputData Data [10]; // input and output data of the sub-thread // ======================== multi threads ================ =============== pthread_create (& p0, NULL, process_0, (void *) & Data [0]); pthread_create (& p1, NULL, process_1, (void *) & Data [1]); pthread_create (& p2, NULL, process_2, (void *) & Data [2]); pthread_create (& p3, NULL, process_3, (void *) & Data [3]); pthread_create (& p4, NULL, process_4, (void *) & Data [4]); pthread_create (& p5, NULL, process_5, (void *) & Data [5]); pthread_create (& p6, NULL, process_6, (void *) & Data [6]); pthread_create (& p7, NULL, process_7, (void *) & Data [7]); pthread_create (& p8, NULL, process_8, (void *) & Data [8]); pthread_create (& p9, NULL, process_9, (void *) & Data [9]); pthread_join (p0, NULL); pthread_join (p1, NULL); pthread_join (p2, NULL); pthread_join (p3, NULL); pthread_join (p4, NULL); pthread_join (p5, NULL ); pthread_join (p6, NULL); pthread_join (p7, NULL); pthread_join (p8, NULL); pthread_join (p9, NULL ); // ========================================== ======== ....}

The above is the general process of my main program. Since pthread_create () only one input parameter is provided to the subthread function when creating a subthread, therefore, when you need to pass multiple function parameters to a sub-function or need to transmit parameters, you can define only one struct and encapsulate all input and output data in the struct, then, pass the reference of the struct variable into the subthread function. In this example, all input and output parameters are packaged in the InputData struct. Ten variables are defined to avoid unnecessary troubles due to ten subthreads, used to input and output function parameters and return values.

The pthread_join (,) function has two parameters. For details, refer to other blog posts. The main function is to let the main thread wait for the end of the sub-thread. The main function is the main thread. If there is no pthread_join code, all sub-threads will be terminated when the main thread ends, without the end of the pipe thread, but in most cases, the main thread will soon end, And the created sub-thread will end with the end of the main thread if it has not been executed, therefore, you must add a code waiting for the end of the sub-thread. Of course, there is also a simpler function pthread_exit (), you can also write phread_exit (0) at the end of all threads to let the main thread wait for the end of all sub-threads.

Phread. h contains many functions, which are not described here. For more information, see https://sourceware.org/pthreads-win32/manual/index.html.

As for how many threads can be created for better efficiency, most of the threads recommended on the Internet and books are twice the number of CPU cores N or 2 * N + 2. Therefore, the relationship between multithreading and hardware is extremely large. If it is a single-core processor, multithreading is not recommended at all, otherwise it will not improve the running efficiency, on the contrary, switching between threads occupies a large part of the overhead.

How much faster does my experiment run ?? The answer is: no improvement.

Let the two machines run the same dataset separately. One uses the multi-threaded method above, and the other executes each group of experiments in a serial manner. After comparison, it is sad to find that the serial experimental group runs faster, of course, it also has a lot to do with the genetic algorithm itself. Each Initialization is different, and the time for finding a solution varies depending on the Variation and crossover probability. However, this result also makes me sad. After two days of multi-threaded programming, I checked various blogs, went to the library to check information, changed the program, and adjusted the program ~~ The results are not satisfactory. Maybe there is a problem with my experiment method or a problem with my program. Anyway, multithreading does not speed up my experiment. However, we will try to use multiple threads or parallel programming to speed up the experiment of genetic algorithms in the future.

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.