Linux thread Learning (1)

Source: Internet
Author: User

Differences between threads and processes

-----------------------------------------------------

1. Frugal: in Linux, when a new process is started, it must be allocated an independent address space and a large number of data tables are created to maintain itsCodeSegment, stack segment, and data segment. Multiple threads running in a process use the same address space for each other to share most of the data. The space required to start a thread is much less than the space required to start a process, in addition, the time required for switching between threads is much less than the time required for switching between processes.

2. Convenient communication: different processes have independent data spaces and data transmission can only be performed through communication. This method is not only time-consuming but also inconvenient. The thread is not the case. Because the threads in the same process share data space, the data of one thread can be directly used by other threads, which is fast and convenient.

 

Advantages

-----------------------------------------------------

1) Improve applicationsProgramResponse. This is especially meaningful for graphic interface programs. When an operation takes a long time, the entire system will wait for this operation. At this time, the program will not respond to keyboard, mouse, and menu operations, but will use multithreading technology, putting time consuming in a new thread can avoid this embarrassing situation.
2) Make the multi-CPU system more effective. The operating system ensures that different threads run on different CPUs when the number of threads is not greater than the number of CPUs.
3) Improve the program structure. A long and complex process can be considered to be divided into multiple threads and become several independent or semi-independent running parts. Such a program will facilitate understanding and modification.

 

Thread Programming

----------------------------------------------------

Multithreading in Linux follows the POSIX thread interface, which is called pthread. To compile a multi-threaded program in Linux, you need to use the header file pthread. H. You need to use the library libpthread. A for connection.

 

# Include <pthread. h>
# Include <stdio. h>

Void thread (void)
{
Int I;
For (I = 0; I <3; I ++)
Printf ("This Is A pthread./N ");
}

Int main (void)
{
Pthread_t ID;
Int I, RET;
Ret = pthread_create (& ID, null, (void *) thread, null );
If (Ret! = 0 ){
Printf ("create pthread error! /N ");
Exit (1 );
}
For (I = 0; I <3; I ++)
Printf ("this is the main process./N ");
Pthread_join (ID, null );
Return (0 );
}

 

Gcc-O thread-LpthreadTest//PthreadThe library is not the default library in Linux. You need to use the library libpthread. A during connection,

Run thread and we get the following results:
This is the main process.
This is a pthread.
This is the main process.
This is the main process.
This is a pthread.
This is a pthread.
Run thread again and we may get the following results:
This is a pthread.
This is the main process.
This is a pthread.
This is the main process.
This is a pthread.
This is the main process.

 

The two results are different, which is the result of two threads competing for CPU resources.

 

Thread Functions

----------------------------------------------------------------

Int pthread_create (pthread_t * restrictTIDP, Const pthread_attr_t * restrictATTR,
Void *(*Start_rtn) (Void), void * restrictARG);

 

The first parameter is the pointer to the thread identifier.
The second parameter is used to set the thread attributes.
The third parameter is the starting address of the thread-running function.
The last parameter is the parameter used to run the function.

 

Note: If you want to pass a parameter to a thread function, you can use the parameter Arg, whose type is void *. If you need to pass multiple parameters, you can take these parameters into a struct for transmission. In addition, because the type is void *, your parameters cannot be released in advance.

 

Int pthread_join (pthread_t thread, void ** value_ptr );

The thread blocking function suspends the current thread, waits for the specified thread to end, and recycles resources.

 

 

Void pthread_exit (void * rval_ptr)

There are three ways to exit a single thread: return from the startup routine, the return value is the exit code of the thread; be canceled by other threads; the thread calls pthread_exit.

Rval_ptr is a non-type pointer, which is equivalent to the exit code of the thread. It is accessed through pthread_join ()

 

Void * thr_fn1 (void * Arg)
{
Printf ("thread 1 returning/N ");
Return (void *) 1 );
}

Void * thr_fn2 (void * Arg)
{
Printf ("thread 2 exiting/N ");
Pthread_exit (void *) 2 );
}

pthread_create (& tid1, null, thr_fn1, null);
pthread_create (& tid2, null, thr_fn2, null);
pthread_join (tid1, & tret); // tret = 1
pthread_join (tid2, & tret); // tret = 2

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.