User-configured API (Pthread)

Source: Internet
Author: User
Tags posix

int Pthread_create ((pthread_t *thread, pthread_attr_t *attr, void * (*start_routine) (void *), void *arg)
If thread creation succeeds, 0 is returned. If thread creation fails, an error number is returned, and the content in *thread is undefined

int Pthread_join (pthread_t thread, void **retval);
Waits for threads specified by thread to end in a blocking manner. When the function returns, the resource that is waiting for the thread is retracted. If the thread has ended, the function returns immediately. and thread-specific threads must be joinable

int Pthread_detach (pthread_t tid);
After calling Pthread_join (pthread_id), if the thread is not running at the end, the caller will be blocked, in some cases we do not want this, such as in the Web server when the main thread for each new link to create a child thread for processing, The main thread does not want to be blocked by calling Pthread_join (because it has to continue processing the incoming link), you can add code to the child thread
Pthread_detach (Pthread_self ()) or parent thread calls Pthread_detach (thread_id) (non-blocking, can be returned immediately)

pthread_t pthread_self (void);
Gets the ID of the thread itself. The type of pthread_t is unsigned long int, so use%lu mode when printing, otherwise the display result is faulty.

void Pthread_exit (void* retval);
The thread terminates execution by calling the Pthread_exit function as if the process had called the Exit function at the end. The purpose of this function is to terminate the thread that invokes it and return a pointer to an object.

int Pthread_cancel (pthread_t thread)
Sends a terminating signal to thread, and returns 0 if successful, otherwise a value other than 0. Sending a success does not mean that thread will terminate.
Http://www.cnblogs.com/lijunamneg/archive/2013/01/25/2877211.html

int Pthread_attr_init (pthread_attr_t *attr);
int Pthread_attr_destroy (pthread_attr_t *attr);
The function is to initialize the properties of a thread object and need to be stripped of its initialization with the Pthread_attr_destroy function.
The threading attribute pthread_attr_t in POSIX threads mainly includes the scope property, the Detach property, the stack address, the stack size, and the priority.

int Pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize);
attr is a thread attribute variable; stacksize is the stack size set. The return value 0,-1 indicates success and failure, respectively.
The stack minimum value is defined as Pthread_stack_min, which contains # include <limits.h> can be viewed by printing its value. For default values can be passed pthread_attr_getstacksize (&attr, &stack_size); Print Stack_size to view.

int Pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
Set the thread scheduling policy; The POSIX standard specifies three scheduling policies: first-in, first-out (SCHED_FIFO), circular (SCHED_RR), and Custom (Sched_other). Sched_fifo is a queue-based scheduler that uses a different queue for each priority. SCHED_RR are similar to FIFO, but each thread of the former has an execution time quota. Sched_fifo and SCHED_RR are extensions to the POSIX Realtime. Sched_other is the default scheduling policy.

int Pthread_attr_setscope (pthread_attr_t* attr, int scope);
The POSIX standard defines two values: Pthread_scope_system and pthread_scope_process, which represent competing CPU time with all threads in the system, which indicates that only the threads in the same process compete with the CPU. The default is pthread_scope_process.

int Pthread_attr_setdetachstate (pthread_attr_t* attr, int detachstate);
This indicates whether the new thread is out of sync with the other threads in the process, and if set to pthread_create_detached, the new thread cannot be synchronized with Pthread_join () and frees the resource itself when exiting. The default is pthread_create_joinable state. This property can also be set with Pthread_detach () after the thread is created and run, and once set to the Pthread_create_detach state (whether set at creation or runtime), it cannot be restored to Pthread_create_ joinable status.

int Pthread_attr_setschedparam (pthread_attr_t* attr, struct sched_param* param);
int Pthread_attr_getschedparam (pthread_attr_t* attr, struct sched_param* param);
A struct SCHED_PARAM structure that currently has only one sched_priority integer variable that represents the thread's run priority. This parameter is valid only if the scheduling policy is real-time (that is, SCHED_RR or SCHED_FIFO) and can be changed at run time by the Pthread_setschedparam () function, which defaults to 0

int Pthread_setschedparam (pthread_t target_thread, int policy, const struct Sched_param *param)
Used to set the calling policy and priority of the thread.


Note:
1. header file
#include <pthread.h>
2. Compiling
The Pthread library is not the default library for Linux systems and requires the use of static library libpthread.a when connecting, so the thread functions need to be connected to library functions at compile time, such as GCC xx.c-lpthread

User-configured API (Pthread)

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.