執行緒中的一些函式

2021-08-22 15:20:13 字數 2517 閱讀 9863

在linux下用pcb來模擬的,輕量級程序

作業系統cpu排程的乙個基本單位

是乙個程式的執行線路

int pthread_create(pthread_t *thread, //id

const pthread_attr_t *attr, //設定執行緒的屬性,attr為null表⽰使⽤預設屬性

void *(*start_routine) (void *), //函式位址null

void *arg); //入口函式攜帶的引數

執行緒終止有三種方法

1. 從執行緒函式return。這種⽅法對主線程不適⽤,從main函式return相當於調⽤exit。

2.執行緒可以調⽤pthread_ exit終⽌⾃⼰。

void pthread_exit(void *value_ptr);
3.⼀個執行緒可以調⽤pthread_ cancel終⽌同⼀程序中的另⼀個執行緒。

int pthread_cancel(pthread_t thread); //取消乙個執行中的執行緒

//成功返回0;失敗返回錯誤碼

int pthread_join(pthread_t thread,	//執行緒id

void **value_ptr //指向執行緒的返回值);

//成功返回0;失敗返回錯誤碼

int pthread_detach(pthread_t thread);

//可以是執行緒組內其他執行緒對目標執行緒進行分離,也可以是執行緒自己分離:

pthread_detach(pthread_self());

int pthread_mutex_init(pthread_mutex_t *restrict mutex,//要初始化的互斥量

const pthread_mutexattr_t *restrict attr    //null);

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

int pthread_mutex_destroy(pthread_mutex_t *mutex);
死鎖產生的四個條件

1.互斥條件

2.不可剝奪條件

3.請求與保持

4.環路等待條件

int pthread_cond_init(pthread_cond_t *restrict cond, //要初始化的變數

const pthread_condattr_t *restrict attr        //null);

int pthread_cond_wait(pthread_cond_t *restrict cond,    //在這兒等待

pthread_mutex_t *restrict mutex);    //互斥量

int pthread_cond_broadcast(pthread_cond_t *cond);

int pthread_cond_signal(pthread_cond_t *cond);

int pthread_cond_destroy(pthread_cond_t *cond)
#include int sem_init(sem_t *sem, int pshared,//0表示執行緒間共享,非零表示程序間共享

unsigned int value);    //訊號量初始化

int sem_wait(sem_t *sem)
int sem_post(sem_t *sem);
int sem_destroy(sem_t *sem);
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

一些執行緒相關的函式

object類自帶函式synchronized obj 用wait 寫出生產者和消費者模式的乙個小例子 生產者執行緒 synchronized queue catch exception e 沒滿,生成元素放入佇列 queue.add element queue.notifyall 喚醒所有其他正在...

執行緒中的一些簡單總結

簡單的總結 為什麼擴容的數量都是2的冪指數呢 這樣做可以加快速度,很多節點在擴容前後都是可以保持不變的 concurrenthashmap的弱一致性?在get key 的時候是在鍊錶進行遍歷,有可能返回的是過時的資料。那怎麼保持強一致性呢?collections.synchronizedmap 方法...

openCV中的一些函式

把scr的元素與常量value相加放到dst裡。如果mask沒有被設為null,那麼mask中非零元素指定的dst元素值在函式執行後不變。void cvadds const cvarr scr,cvscalar value,cvarr dst,const cvarr mask null 這個 cvs...