UNIX Environment Advanced Programming Learning Note (vii): Threads

Source: Internet
Author: User
Tags mutex

1 threads contain thread IDs, a set of register values, stacks, scheduling priorities and policies, signal-masking words, errno variables, and thread-private data. All the information for the process is shared for all threads of the process, including executable text, program global memory and heap memory, stack, and file descriptors.

Thread can get its own thread ID through the Pthread_self function

#include <pthread.h>pthread_t pthread_self (void)


New processes can be created with the Pthread_create function

#include <pthread.h>int pthread_create (pthread_t *restrict TIDP,                     const pthread _attr_t *Restrict attr,                   void * (*START_RTN) (voidvoid *restrict Arg)


The memory unit that TIDP points to is set to the thread ID of the newly created thread.
The attr is used to specify the properties of the thread, and now it is temporarily set to null.
The thread runs from the address of the START_RTN function, STRAT_RTN only has an untyped pointer argument arg, and if there are multiple arguments, put them into the struct, passing in ARG

2 Thread termination

#include <pthread.h>void pthread_exit (void *roal_ptr)



Other threads in the same process can be canceled through the Pthread_cancel function

void pthread_cancel (pthread_t tid)



3 Thread Synchronization
Create/Eliminate Mutex amount

int pthread_mutex_init (pthread_mutex_t *restrict Mutex,                        const pthread_mutexattr_t * restrict attr); int Pthread_mutex_destroy (pthread_mutex_t *<pthread.h>int Pthread_mutex_lock ( pthread_mutex_t *mutex); int pthread_mutex_trylock (pthread_mutex_t *mutex); int pthread_mutex_unlock (pthread_mutex_t *mutex);

UNIX Environment Advanced Programming Learning Note (vii): Threads

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.