C Review-use POSIX standard thread library porgramming with pthread

Source: Internet
Author: User
Tags ftp site

Programming with pthread? What is pthread?

Pthread, as the standard thread library for C/C ++ programming, is actually POSIX standard thread library.

 

So how to understand the POSIX standard?

Below are links to some materials. If you are interested, you can view them by yourself.

1. http://zh.wikipedia.org/wiki/POSIX

2. standards.ieee.org/findstds/standard/1003.1-2008.html

3. www.opengroup.org/austin/papers/posix_faq.html

4. www.unix.org/version3/ieee_std.html

 

Where can I find the pthread source code implementation?

If you want to literate or learn pthread, you can download the glibc source code, which includes the implementation of pthread.

Gnu ftp site link: http://ftp.gnu.org/gnu/glibc

 

Pthread APIs:

Pthread APIs can be divided into the following four parts:

1. thread management: operations on threads, such as creation, suspension, connection, and other operations on thread attributes.

2. mutexes: mutex lock, which supports synchronization.

3. Condition vars: Condition lock, communication between threads sharing mutex locks.

5. sync: Manage Synchronization

There are still a lot of Chinese learning materials about pthread, so here I will not be able to introduce more of those learning knowledge. If you are a newbie,

So please read the relevant materials and books before learning. Please forgive me ~


Pthread_exit and pthread_join.

I remember one day before the test, my graduate student asked me a question: he said that when he was using the pthread library, the worker thread he created didn't work. Let me help

Now I forget what he wrote.CodeBut I still remember the problem, so I can use another small example instead:

 

# Include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <pthread. h> # define thread_nums 20 // workervoid * greeting (void * MSG); int main (INT ARGs, char ** argv) {pthread_t workers [thread_nums]; int retval; int index; char * MSG = "Hello pthread! "; For (Index = 0; index <thread_nums; index ++) {retval = pthread_create (& Workers [Index], null, greeting, (void *) MSG ); if (retval =-1) {printf ("% s \ n", "error! "); Exit (-1) ;}// pthread_exit (null); Return exit_success;} void * greeting (void * MSG) {printf (" thread # % lD, greeting % s \ n ", (long) pthread_self (), (char *) MSG); usleep (1000000 );}

 

I noticed pthread_exit (null) in the above Code. In fact, it is very easy to predict this smallProgramWhat is the situation? Only a part of the created thread is complete.

Another thread has lost the running environment before the job is finished, because the main thread has exited.

Well, the following is the function of pthread_exit (null), which is to wait for all sub-threads to exit.

Pthread_join is similar to pthread_exit, but the exit status code can be obtained from the exited subthread.

 

Note:

When the share memory method is used for communication between threads, all data write operations in the shared area must be locked. Of course, read operations are not required ~

The common usage is as follows:

 
Pthread_mutex_lock (& mutex); //... // pthread_mutex_unlock (& mutex );

 

 

During lock competition, which thread will win when the mutex lock is used? This problem involves thread priority scheduling. If priority scheduling is not set

The lock is random, that is, unpredictable.

 

Mutual Exclusion lock and conditional lock used together:

Many of the conditional Lock operation functions are related to mutex locks and need to be used together. If only mutex locks are used, as mentioned above, priority scheduling is not used.

In this case, the thread may be unpredictable to obtain the lock. from another point of view, we cannot achieve communication between specified threads. For example, after thread a completes, You need to specify a line B to complete a part.

Work, if only mutex lock is used, this cannot be completed, because it cannot be guaranteed that after thread a releases the lock, B can immediately obtain the lock. Therefore, conditional locks are required.

The condition lock is generally used as follows:

 

 
Void * thread_work_func1 (void * parm) {for (;) {pthread_mutex_lock (& mutex); If (XXXX) {pthread_cond_signal (& condition );} else {//....} pthread_mutex_unlock (& mutex) ;}} void * thread_work_fun2 (void * parm) {for (;) {pthread_mutex_lock (& mutex); pthread_cond_wait (& condition, & mutex ); // do something... pthread_mutex_unlock (& mutex );}}

 

 As you need to read the glib source code later, some of the threads are about pthread, So let's review it. In the subsequentArticle, I will introduce pthread again.

We will consider writing more details.

 

 

 

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.