Pthread learning notes (2)-pthread common APIs and a brief introduction

Source: Internet
Author: User

After a preliminary study of the previous document, I had a simple perceptual knowledge of pthread, but I still had little understanding of pthread, this document mainly describes some common pthread APIs.

 

The first is the pthread creation API: pthread_create

# Include <pthread. h> <br/> int pthread_create (pthread_t * thread, // The thread to be created <br/> pthread_attr_t * ATTR, // attributes of the thread <br/> void * (* start_routine) (void *), // function pointer to be executed by the thread <br/> void * Arg // The parameter passed to start_routine <br/> );

When the function is successfully created, 0 is returned, and the created thread TID is written to the passed thread pointer (the first parameter). Otherwise, a non-zero value is returned and errno is set.

The first and third parameters must be set, and the other two parameters can be set as needed. If there is no need, enter null.

 

 

Pthread_exit: Terminate the current thread

 

# Include <pthread. h> <br/> void pthread_exit (void * retval); <br/> // This function is used to exit the current thread, before exiting, call pthread_cleanup_push <br/> // the above functions will be described below. <Br/> // This function is implicitly called in the upper-layer function of the thread. You can add a retval parameter. <br/> // display the call for pthread_join function reference.

 

Pthread_join: suspends the current thread until the specified thread is terminated (this function is very important to pthread, as described in the previous document, if you do not call this function, subsequent create failures may occur ).

# Include <pthread. h> <br/> int pthread_join (pthread_t thread, void ** thread_return); <br/> // thread_return is the parameter at the end of th, <br/> // null if it is not explicitly specified <br/> // you need to check the usage of this parameter.

 

Pthread_cancel: cancels a thread.

# Include <pthread. h> <br/> // cancel the thread <br/> int pthread_cancel (pthread_t thread ); <br/> // used to set the revocation status of the current thread <br/> // pthread_cancel_enable allows revocation <br/> // pthread_cancel_disable ignores revocation <br/> // to avoid being canceled in addition, the thread uses pthread_cancel to cancel <br/> // oldstate to save the previous state, to restore <br/> int pthread_setcancelstate (INT state, int * oldstate); <br/> // you can set the revocation type of the current thread, including: <br/> // pthread_cancel_asynchronous undo now <br/> // pthread_cancel_deferred delays to the Undo point <br/> int pthread_setcanceltype (INT type, int * oldtype); <br/>

 

 

Thread attribute: (the second parameter of the Create Function in the preceding article, type: pthread_attr_t, which can be called by the pthread_attr_xxxx Function Family)

 

Detachstate: separated or cut-in State. Two values are available: pthread_create_joinable (default value) and phtread_create_detached.

 

Schedpolicy: scheduling policy; values: sched_other and sched_fifo

 

Schedparam: scheduling policy-related

 

Inheritsched: pthread_explicit_sched (default value), pthread_inerit_sched

 

Scope: time slice; values: pthread_scope_system (default value: one system time slice for each thread) and pthread_scope_progcess (the thread shares the system time slice ).

 

 

Pthread cleanup macro

 

The pthread cleanup macro is mainly used to process the launch status of a thread. pthread_exit and pthread_join can be used as its parameters.

# Include <pthread. h> <br/> // These are macro definitions. For details, see/usr/include/pthread. h <br/> void pthread_cleanup_push (void (* routine) (void *), void * Arg); <br/> void pthread_cleanup_pop (INT execute ); <br/> void pthread_cleanup_push_defer_np (void (* routine) (void *), void * Arg); <br/> void pthread_cleanup_pop_restore_np (INT execute ); <br/> // do not have a deep understanding of these macros. For more information, see <br/>. <Br/> // these macros are used to release related resources at the end of the thread. pthread_exit is used to call pthread_cleanup. <br/> // when processing, pthread_cleanup_pop is used to pop up from the stack.

 

Mutex

Because the thread is executed concurrently, sometimes some data needs to be protected. For example, if the standard output of Multithreading is not processed, the output will basically be garbled, in this case, mutex can be used to control the output stream. When a thread executes the write operation, a mutex lock is applied to prevent other threads from simultaneously writing data.

 

The mutex object is defined as pthread_mutex_t in pthread. The following are some of its API functions:

# Include <pthread. h> <br/> // create a mutex object and initialize the mutex object with the specified initialization attribute <br/> // attributes include: <br/> // pthread_mutex_initializer fast mutex, simple addition and unlock <br/> // pthread_recursive_mutex_initializer <br/> // recursive mutual exclusion, lock count, pthread_mutex_unlock of the same number of times is required for unlocking <br/> // pthread_errorcheck_mutex_initializer <br/> // create check error mutex, this mutex will attempt to lock the thread after being locked and return a <br/> // edeadlk error code without blocking <br/> int pthread_mutex_init (pthread_mutex_t * mutex, <br/> const pthread_mutex_attr_t * mutexattr); <br/> // lock <br/> int pthread_mutex_lock (pthread_mutex_t * mutex ); <br/> // unlock <br/> int pthread_mutex_unlock (pthread_mutex_t * mutex); <br/> // lock, however, if the object is locked, the ebusy error code is returned without blocking <br/> int pthread_mmutex_trylock (pthread_mutex_t * mutex ); <br/> // analyze and release mutex-related resources <br/> int pthread_mutex_destroy (pthread_mutex_t * mutex); <br/> // if the above functions are successful, 0 is returned, consistent with General Functions

 

Condition variable

The thread uses a condition variable object to block itself and wait for a specific condition to occur.

The condition object is defined as pthread_cond_t.

# Include <pthread. h> <br/> // create a pointer to a condition variable. You can use pthread_cond_initializer in Linux. <br/> pthread_cond_t cond = pthread_cond_initializer; <br/> int pthread_cond_init (pthread_cond_t * cond, <br/> pthread_condattr_t * cond_attr); <br/> // structure <br/> int pthread_cond_destroy (pthread_cond_t * Cond ); <br/> // two signal functions <br/> int pthread_cond_singal (pthread_cond_t * Cond); <br/> int pthread_cond_broadcast (pthread_cond_t * Cond); <br />// Two waiting conditions. Otherwise, the suspended function <br/> // suspends the thread when the current expected condition does not arrive, unlock mutex <br/> // call the thread when the condition is set up and lock the mutex <br/> int pthread_cond_wait (pthread_cond_t * cond, <br/> pthread_mutex_t * mutex ); <br/> // the absolute time of the abstime parameter and the time ()-compatible return value, <br/> // number of seconds since January 1, Unix epoch time <br/> // if the condition did not occur before the time, the etimeout error is returned. <br/> int pthread_cond_wimewait (pthread_cond_t * cond, <br/> pthread_mutex_t * mutex, <br/> const struct timespec * abstime ); <br/> // both functions are set. Is the withdrawal point (what is the meaning of this withdrawal point ?)

 

So far, the basic pthread API functions have been learned here. In the future, we will learn more in depth with examples.

 

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.