Pthread learning notes (3)-Guide to POSIX Thread Programming on a journey (1)

Source: Internet
Author: User

The previous understanding of the property object pthread_attr_t is very simple. I have found some useful information from the Internet.

 

_ Detachstate indicates whether the new thread is out of sync with other threads in the process. If it is set to a bit, the new thread cannot use pthread_join () for synchronization and is released upon exit.
Resources used
. The default value is pthread_create_joinable. This attribute can also be set by pthread_detach () after the thread is created and run.
But once it is set to pthread_create_detach status (whether it is set at creation or runtime), it cannot be restored
Pthread_create_joinable status
.

 

_ Schedpolicy indicates the scheduling policy of the new thread, including sched_other (normal, non-real-time), sched_rr (real-time, rotation method), and
Sched_fifo (real-time, first-in-first-out). The default value is sched_other. The last two scheduling policies are only valid for Super Users.
. It can be used during runtime
Pthread_setschedparam () to change.

 

_ Schedparam, a struct
Sched_param structure. Currently, only one sched_priority integer variable represents the thread running priority.
(What is the priority granularity ?). This parameter is only used when the scheduling policy is real-time (that is, sched_rr
Or sched_fifo ).
And can be changed through the pthread_setschedparam () function at runtime. The default value is 0.

 

_ Inheritsched. Two values are available: pthread_explicit_sched and pthread_inherit_sched. The former table
Indicates that the new thread explicitly specifies the scheduling policy and scheduling parameters (that is, the value in ATTR), while the latter indicates inheriting the value of the caller thread.
. The default value is pthread_explicit_sched.

 

_ Scope indicates the range of CPU competition among threads, that is, the valid range of thread priority.
. The POSIX standard defines two
Value: pthread_scope_system and pthread_scope_process. The former indicates the CPU time to compete with all threads in the system, and the latter indicates that only
Competing CPU with threads in the same process
. Currently, linuxthreads only implements the pthread_scope_system value.

 

There are some values in the pthread_attr_t structure, but pthread_create () is not used.

To set these attributes, POSIX defines a series of attribute setting functions, including pthread_attr_init (), pthread_attr_destroy (), and pthread_attr_get ---/pthread_attr_set --- functions related to each attribute.

 

Definition in pthreadtypes:

Typedef Unio

{

Char _ SIZE [56];

Long int align;

} Pthread_attr_t;

 

Linux thread implementation is performed outside the kernel
In the kernel, the interface do_fork () for creating processes is provided (). The kernel provides two system calls: _ clone () and
Fork (), and finally calls the do_fork () core API with different parameters. Of course, to implement threads, there is no core support for multi-process (actually Lightweight Process) shared data segments.
Therefore, do_fork () provides many parameters, including clone_vm (shared memory space), clone_fs (shared file system information ),
Clone_files (shared file descriptor table), clone_sighand (shared signal handle table), and clone_pid (shared process ID, only for processes in the kernel, that is, no. 0
Valid process ). When the fork system is used for calling, the kernel calls do_fork () without any shared attributes. The process has an independent running environment and uses
When pthread_create () is used to create a thread, all these attributes are finally set to call _ clone (), and all these parameters are passed to the do_fork () in the kernel, from
The created "process" has a shared running environment. Only the stack is independent and passed in by _ clone.

Linux threads exist in the kernel in the form of lightweight processes.
, With independent table items, and all creation, synchronization, deletion, and other operations are performed in the external pthread library. Pthread
The library uses a management thread (_ pthread_manager (), each process is independent and unique) to manage the creation and termination of threads, allocate thread IDs to threads, and send thread-related signals.
(For example, cancel), while the caller of the main thread (pthread_create () transmits the request information to the management thread through the pipeline.
.

 

 

Thread canceled


2.1 Definition of thread Cancellation

Generally, the thread automatically terminates when the main function exits.
But it can also be forcibly terminated by receiving a termination (Cancellation) request from another thread.
.

 

2.2 semantics of thread Cancellation

The method for canceling a thread is to send a cancel signal to the target thread, but the method for processing the cancel signal is determined by the target thread.
, Or ignore, or terminate immediately, or continue running to cancelation-point (cancel point)
, Determined by different cancelation statuses
.

Default processing of cancel signal received by the thread
(That is, the default status of the pthread_create () Creation thread) is to continue running until the cancellation point
In other words, set a canceled state, and the thread continues to run. The thread will exit only when it runs to the cancelation-point.

 

2.3 cancellation point

According to POSIX standards, pthread_join (), pthread_testcancel (),
Pthread_cond_wait (), pthread_cond_timedwait (), sem_wait (), sigwait (), and other functions
System calls that cause blocking, such as read () and write (), are all cancelation-point
And other pthread functions will not cause
Cancelation action. However, the pthread_cancel manual page claims that, due to the poor combination of linuxthread library and C library, C library functions are currently not
Cancelation-point
But the cancel signal will cause the thread to exit from the blocked system call and reset the eintr error code.
Therefore, you can
The cancelation-point system calls pthread_testcancel () before and after calling, so as to meet the POSIX standard requirements.
, That is, the following code segment:

Pthread_testcancel (); <br/> retcode = read (FD, buffer, length); <br/> pthread_testcancel ();

The pthread_testcancel function declaration is as follows:

// Terminate the thread as per pthread_exit (pthread_canceled) If <br/> // It has been canceled <br/> extern void pthread_testcancel (void );

From the code description above, we can see that when the cancellation point is reached, pthread_testcancel () is used to solve the problem of poor integration with the C library function () you can perform the exit Operation (when pthread_canceled is set, the pthread_exit function is called ).

 

2.4 program design considerations

If the thread is in an infinite loop and the loop body does not execute the path to the cancellation point
The thread cannot be terminated by canceling the request from other external threads. Therefore, you must add the pthread_testcancel () call to the path required for such a loop body.
(Prevent exceptions that cannot be jumped out ).

 

2.5
Pthread functions related to thread Cancellation

 

Int pthread_cancel (pthread_t thread)

Send the termination signal to the thread. If the signal is successful, 0 is returned. Otherwise, the value is not 0. Sending successful does not mean that thread will terminate
(Consider the thread status and the location of the cancellation point ).

 

Int pthread_setcancelstate (INT state, int * oldstate)

Set the response of this thread to the cancel Signal
The State has two values: pthread_cancel_enable (default) and
Pthread_cancel_disable, indicating that the signal is set to the cancled status and the cancel signal is ignored to continue running.
; Old_state if not
Null is saved to the original cancel state for recovery.
.

(General function design principle -- return value indicates the function execution status, and input and output results are passed in by the parameter list)

 

Int pthread_setcanceltype (INT type, int * oldtype)

Set the execution time of the canceling action of this thread. The type can be pthread_cancel_deffered or
Pthread_cancel_asychronous, valid only when the cancel status is enable
, Indicating that after receiving the signal, the system continues to run until the next cancellation point and then exits.
Cancel now (Exit)
If the value of oldtype is not null, it is saved to the canceled action type value of the shipping.

 

Void pthread_testcancel (void)

Check whether the thread is in the canceld status. If yes, cancel the operation.
Otherwise, return directly.

 

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.