UNIX Programming (VI) threading

Source: Internet
Author: User

Judge Threads the same

Pthread_equal (pthread_t tid1, pthread_t Tid2);


Get your own thread ID

pthread_t pthread_self (void)


Thread creation

int pthread_create (pthread_t *restrict TIDP, const pthread_attr_t *restrict attr, void * (*START_RTN) (void *), void *resttr ICT arg);

The new thread ID is set TIDP points to memory, attr sets the thread properties, the address of the START_RTN function, the ARG function into the parameter, and, if a parameter is used, the struct body. Creates a new thread that inherits the signal mask word of the calling thread, and the pending signal assembly is removed.


Thread termination

1. Code Back

2. Canceled by another thread

3. Call Pthread_exit

void Pthread_exit (void *rval_ptr) End thread

The Rval_ptr parameter is an untyped pointer that represents the thread return value


int Pthread_join (pthread_t thread, void **rval_ptr); The current thread waits for the specified thread to end

Rval_ptr can make the thread normal to the return value of the code, or it can be a pthread_exit specified value.


Threads can cancel other threads through Pthread_cancel requests

int Pthread_cancel (pthread_t tid);


Thread Cleanup handlers:

void Pthread_cleanup_push (void (*RTN) (void *), void *arg);

void Pthread_cleanup_pop (int execute);

These cleanup functions are called by the Pthread_cleanup_push function to register the thread cleanup function and finally by pthread_exit or by other threads calling the Pthread_cancel thread to terminate. Normal termination of threads does not call these cleanup functions.


Thread synchronization Mutex

int Pthread_mutex_init Initialization

int Pthread_mutex_destroy Destroy


int Pthread_mutex_lock Holding Lock

int Pthread_mutex_trylock try to hold the lock, if there is a lock, do not block, return directly

int Pthread_mutex_unlock Release Lock

int Pthread_mutex_timedlock and latch-like, auto-release lock to TIME


Methods to avoid deadlocks, all threads are locked in the same order


Read/write Lock

It is a thread that writes when other threads are blocked, but no write threads are read, no locks are held, and functions are similar to ordinary lock functions.

There are also read and write locks with timeouts.


Condition variable

int Pthread_cond_init Initialization

int pthread_cond_destory Destroy

int pthread_cond_wait Release the lock, the thread waits until the condition changes (wait for the condition to change, must already hold the lock)

int pthread_cond_timewait and similar above, just have a timeout do not wait

int pthread_cond_signal sends a signal to change the condition, at least to wake up a

int Pthread_cond_broadcast wakes all the threads waiting for the condition


Barrier

Allow each thread to wait until all of the cooperating threads reach a certain point and then continue to execute

int Pthread_barrier_init Here is a count parameter that represents the total number of threads to wait

int Pthread_barrier_destory

int pthread_barrier_wait waits for a thread (sleep) until all threads wait for the count to be met, and all threads are awakened




UNIX Programming (VI) threading

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.