linux 线程

2021-03-06, updated 2021-09-12

pthread 相关函数

pthread_sigmask

用作在主调线程里控制信号掩码。

#include <signal.h>

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);

Compile and link with -pthread.

参数

返回值

pthread_detach()

pthread_detach - detach a thread

分离子线程,线程运行结束时自动回收资源

#include <pthread.h>

int pthread_detach(pthread_t thread);

// Compile and link with -pthread.

return value: On success, pthread_detach() returns 0; on error, it returns an error number.

pthread_self

pthread_self - obtain ID of the calling thread

#include <pthread.h>

pthread_t pthread_self(void);

Compile and link with -pthread.

return value

This function always succeeds, returning the calling thread’s ID.

pthread_mutex

/* Thread identifiers.  The structure of the attribute type is not
   exposed on purpose.  */
// bits/pthreadtypes.h
typedef unsigned long int pthread_t;

pthread_mutex_t  write_lock;
pthread_mutex_init(&write_lock, NULL);
pthread_mutex_lock(&write_lock);    
pthread_mutex_unlock(&write_lock);
pthread_mutex_destroy(&write_lock);

pthread遇到的问题

调用pthread_cancel函数请求后,等到响应请求时,代码调用了pthread_clean_push函数中的clean_fun1和clean_fun2,函数clean_fun2中的语句先被打印。

words: 379 tags: pthread c