Thread of the Linux learning note

Source: Internet
Author: User

First, the basic knowledge

1: basic knowledge.

1, threads need information such as: Thread ID, register, stack, scheduling priority and policy, signal mask Word, errno variable and thread private data.

2, all the information for the process is shared for all threads.

3, whether there are multiple threads supported in the following two ways test:

1) Compile-time determination: #ifdef _posix_threads

2) Runtime OK: sysconf function call _sc_threads is commonly used.

4, Thread ID.

1) in a process, the thread ID is unique. Thread ID dependencies and processes only make sense.

2) Thread ID representation type: pthread_t type

5. Steps for variable increment operations:

1) variable values are read into the register from the memory unit.

2) Modify the variables in the register.

3) Write the value of the variable back to the memory unit

4) Therefore, when multiple threads attempt to modify the same variable at the same time, synchronization is required.

2: Thread creation and termination.

1, thread creation.

1) does not determine which thread runs first (similar to child process creation).

2) The error code is usually returned after the CREATE function creates a failure.

7, thread termination.

1) Any thread calls exit, _exit, and the _exit function will terminate the process. (so it is not possible to terminate a single thread using this method)

2) return from the start routine. The thread exit code when the value is returned.

3) can be canceled by other threads in the same process.

4) The thread calls the Pthread_exit function.

3: Process and thread function/function table

Process Primitive Line Hodohara language Describe

Fork

Pthread_create Create a new control flow
Exit Pthread_exit Binate exits with control flow
Waitpid Pthread_join Get exit status from the control flow
Atexit Pthread_cancel_push Registers the function that is called when exiting the control flow
Getpid Pthread_self Gets the ID of the control flow
Abort Pthread_cancel The request control flow is not properly exited.

Second, the correlation function.

1: Thread manipulation function.

1Compares the thread ID size.IntPthread_equal (pthread_t tid1, pthread_t Tid2)2Gets the own thread ID. pthread_t Pthread_self (void);3Creates a thread.int pthread_create (pthread_t *restrict TIDP,Const pthread_attr_t *restrict attr,void * (*START_RTN) (void *),void *Restrict Arg);1Parameter arg is used to customize a variety of different thread properties4Thread termination.void Pthread_exit (void *RVAL_PTR);5Gets the thread exit status.int Pthread_join (pthread_t thread,void * *RVAL_PTR);1The parameter rval_ptr contains the return code.6Cancel other threads (used in process)IntPthread_cancel (pthread_t tid);1Just make a request. does not force termination.7The thread cleanup handler (Atexit of a similar process).void Pthread_cleanup_push ( void (*RTN) (void *), arg); void Pthread_cleanup_pop (int   execute);  1 parameter Execute= 0 o'clock, the cleanup function will not be called.  23 These functions are implemented as macros.  You need to be aware of matches such as {}. 4 The cleanup program is registered and executed in the opposite order.  8 detach thread. int Pthread_detach (pthread_t tid); 
9 the above function. Some untyped pointers can pass a number of values, even when a struct is available.

2, Thread property correlation function.

1Thread property initialization and class destructorint Pthread_attr_init (pthread_attr_t *attr);int Pthread_attr_destroy (pthread_attr_t *attr);2 Get/Sets the detach state.int Pthread_attr_getdetachstats (Const pthread_attr_t *restrict attr,int *Detachstate);int Pthread_attr_setdetachstats (pthread_attr_t *attr,int *Detachstate);1 parameter detachstate only two values: Pthread_create_ detached/Joinable.3 Get/Sets the thread Stack property stackaddr.int Pthread_attr_getstack (Const pthread_attr_t *restrict attr,void **restrict stackaddr, size_t *Restrict stacksize);int Pthread_attr_setstack (pthread_attr_t *attr,void *STACKADDR, size_t stacksize);1the STACKADDR thread property is defined as the lowest memory address of the stack. Whether it is the starting position or the end position depends on the development direction of the stack.  Usually the end (stack by high-low)4 gets/ sets the stack Size property stacksize.  int pthread_attr_getstacksize ( const pthread_attr_t *restrict attr, size_t *restrict stacksize); int pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize);   5 Gets/ sets the thread stack at the end of the buffer size guardsize.  int pthread_attr_getguardsize ( const pthread_attr_t *restrict attr, size_t *restrict guardsize); int pthread_attr_setguardsize (pthread_attr_t *attr, size_t guardsize);        

Three

Thread of the Linux learning note

Related Article

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.