This article can be reproduced at will, but the author and source must be indicated.
[Original] Teach you how to design multiple threads in Linux (1)
-- Multi-thread programming in Linux
Original Author: frozen_socker (popsicle)
E_mail: dlskyfly@163.com
ThreadAlso known
Lightweight Process(
Lightweight Process). In traditional UNIX, a process allows another entity to perform a transaction using the fork dispatch process. The cost of a child dispatch process is much higher than that of a thread, especially when information is transmitted between parent and child processes and between child processes, it needs to be communicated using IPC or other methods. Compared with other threads, threads have many advantages, such as creating threads much faster than creating processes, and sharing the same global storage zone among threads in a process. Multithreading in Linux follows the POSIX thread interface, called pthread. in Linux, the header file required by multithreading is <pthread. h>, and the library libpthread. A is used for connection.
Let's write a very simple example:// Example_1.c
# Include <stdio. h>
# Include <pthread. h>
Void * pthread_func_test (void * Arg );
Int main ()
...{
Pthread_t pt1, pt2;
Pthread_create (& pt1, null, pthread_func_test, "this is the thread_one ");
Pthread_create (& pt2, null, pthread_func_test, "this is the thread_two ");
Sleep (1); // The result is not displayed without this sentence.
}
Void * pthread_func_test (void * Arg)
...{
Printf ("% s/n", ARG );
}
Compile the source file:GCC example_1.c-O example-lpthread
Compiling environment:Platform: fc6 version: 2.6.18-1.2798.fc6 Compiler: GCC 4.1.1 20070105 (Red Hat 4.1.1-51)Run the executable file:
./Example
Output result on the terminal:
This is the thread_one
This is the thread_two
In the example of example_1, three threads are generated. The first is the main thread represented by main, and the other two are the two branch threads represented by pt1 and pt2 respectively, these two threads are created by the pthread_create function, and the execution content is written in the stuff in the pthread_func_test function. The function involved in the above example is: pthread_create ()The function prototype is as follows:Int pthread_create (pthread_t * restrict thread,
Const pthread_attr_t * restrict ATTR,
Void * (* start_routine) (void *), void * restrict Arg );
Parameter solution:1. Each thread has its own ID, namely, the thread ID, which can be referred to as tid. What do you think ?... Yes, it's a bit like PID. Its type is pthread_t, and pthread_t is defined in the header file/usr/include/bits/pthreadtypes. h: typedef unsigned long int pthread_t; it can be regarded as a thread identifier. When a new thread is successfully created, the system allocates a TID to the thread and returns the value to the program that calls it through the pointer. 2. ATTR declares the attributes of the thread. The property structure is pthread_attr_t, which is defined in the header file/usr/include/pthread. h. If it is set to null, it means that only the default attributes of the thread can be used here. 3. start_routine indicates the routine to be executed by the newly created thread. The thread starts from calling the function until the thread is terminated by the return function, or the pthread_exit function is called in the function pointed to by start_routine. Start_routine has only one parameter, which is indicated by the ARG pointer. 4. Arg: it is also a pointer, that is, the parameter of the function pointed to by start_routine pointer.Return Value:
If pthread_create is called successfully, 0 is returned. Otherwise, an error code is returned indicating the type of the error.
You are welcome to send an email to me. However, due to the relationship between work and time, I have the right not to answer your questions. Sorry.