線程取消:
/*
#include <pthread.h>
int pthread_cancel(pthread_t thread);
- 功能:取消線程(讓線程終止)
取消某個線程,可以終止某個線程的運行,
但是并不是立馬終止,而是當子線程執(zhí)行到一個取消點,線程才會終止。
取消點:系統(tǒng)規(guī)定好的一些系統(tǒng)調(diào)用,我們可以粗略的理解為從用戶區(qū)到內(nèi)核區(qū)的切換,這個位置稱之為取消點。
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * callback(void * arg) {
printf("chid thread id : %ld\n", pthread_self());
for(int i = 0; i < 5; i++) {
printf("child : %d\n", i);
}
return NULL;
}
int main() {
// 創(chuàng)建一個子線程
pthread_t tid;
int ret = pthread_create(&tid, NULL, callback, NULL);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error1 : %s\n", errstr);
}
// 取消線程 不是立刻取消的
pthread_cancel(tid);
for(int i = 0; i < 5; i++) {
printf("%d\n", i);
}
// 輸出主線程和子線程的id
printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
pthread_exit(NULL);
return 0;
}
線程屬性
/*
int pthread_attr_init(pthread_attr_t *attr);
- 初始化線程屬性變量
int pthread_attr_destroy(pthread_attr_t *attr);
- 釋放線程屬性的資源
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
- 獲取線程分離的狀態(tài)屬性
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
- 設(shè)置線程分離的狀態(tài)屬性
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * callback(void * arg) {
printf("chid thread id : %ld\n", pthread_self());
return NULL;
}
int main() {
// 創(chuàng)建一個線程屬性變量
pthread_attr_t attr;
// 初始化屬性變量
pthread_attr_init(&attr);
// 設(shè)置屬性
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// 創(chuàng)建一個子線程
pthread_t tid;
int ret = pthread_create(&tid, &attr, callback, NULL);
if(ret != 0) {
char * errstr = strerror(ret);
printf("error1 : %s\n", errstr);
}
// 獲取線程的棧的大小
size_t size;
pthread_attr_getstacksize(&attr, &size);
printf("thread stack size : %ld\n", size);
// 輸出主線程和子線程的id
printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());
// 釋放線程屬性資源
pthread_attr_destroy(&attr);
pthread_exit(NULL);
return 0;
}
線程同步
多線程實現(xiàn)賣票:
selltickets.c(沒有加鎖的情況) 線程同步
/*
使用多線程實現(xiàn)買票的案例。
有3個窗口,一共是100張票。
*/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
// 全局變量,所有的線程都共享這一份資源。
int tickets = 100;
void * sellticket(void * arg) {
// 賣票
while(tickets > 0) {
usleep(6000);
printf("%ld 正在賣第 %d 張門票\n", pthread_self(), tickets);
tickets--;
}
return NULL;
}
int main() {
// 創(chuàng)建3個子線程
pthread_t tid1, tid2, tid3;
pthread_create(&tid1, NULL, sellticket, NULL);
pthread_create(&tid2, NULL, sellticket, NULL);
pthread_create(&tid3, NULL, sellticket, NULL);
// 回收子線程的資源,阻塞
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
// 設(shè)置線程分離。
// pthread_detach(tid1);
// pthread_detach(tid2);
// pthread_detach(tid3);
pthread_exit(NULL); // 退出主線程
return 0;
}
互斥鎖 mutex.c
/*
互斥量的類型 pthread_mutex_t
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
- 初始化互斥量
- 參數(shù) :
- mutex : 需要初始化的互斥量變量
- attr : 互斥量相關(guān)的屬性,NULL
- restrict : C語言的修飾符,被修飾的指針,不能由另外的一個指針進行操作。
pthread_mutex_t *restrict mutex = xxx;
pthread_mutex_t * mutex1 = mutex;
int pthread_mutex_destroy(pthread_mutex_t *mutex);
- 釋放互斥量的資源
int pthread_mutex_lock(pthread_mutex_t *mutex);
- 加鎖,阻塞的,如果有一個線程加鎖了,那么其他的線程只能阻塞等待
int pthread_mutex_trylock(pthread_mutex_t *mutex);
- 嘗試加鎖,如果加鎖失敗,不會阻塞,會直接返回。
int pthread_mutex_unlock(pthread_mutex_t *mutex);
- 解鎖
*/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
// 全局變量,所有的線程都共享這一份資源。
int tickets = 1000;
// 創(chuàng)建一個互斥量
pthread_mutex_t mutex;
void * sellticket(void * arg) { //該函數(shù)是一個線程函數(shù),接收一個void*類型的參數(shù),并返回一個void*類型的指針。
// 賣票
while(1) {
// 加鎖
pthread_mutex_lock(&mutex); //通過調(diào)用pthread_mutex_lock函數(shù)來獲取互斥鎖。&mutex是互斥鎖的地址,用于鎖住臨界區(qū),保證線程安全
if(tickets > 0) {
usleep(6000); //通過調(diào)用usleep函數(shù)來使線程暫停6000微秒,相當于6毫秒,模擬賣票的過程。
printf("%ld 正在賣第 %d 張門票\n", pthread_self(), tickets); //長整型(long類型)
tickets--;
}else {
// 解鎖
pthread_mutex_unlock(&mutex);
break;
}
// 解鎖
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
// 初始化互斥量
pthread_mutex_init(&mutex, NULL);
// 創(chuàng)建3個子線程
pthread_t tid1, tid2, tid3;
pthread_create(&tid1, NULL, sellticket, NULL);
pthread_create(&tid2, NULL, sellticket, NULL);
pthread_create(&tid3, NULL, sellticket, NULL);
// 回收子線程的資源,阻塞
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_exit(NULL); // 退出主線程
// 釋放互斥量資源
pthread_mutex_destroy(&mutex);
return 0;
}
死鎖:deadlock1.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
// 創(chuàng)建2個互斥量
pthread_mutex_t mutex1, mutex2;
void * workA(void * arg) {
pthread_mutex_lock(&mutex1);
sleep(1);
pthread_mutex_lock(&mutex2);
printf("workA....\n");
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex1);
return NULL;
}
void * workB(void * arg) {
pthread_mutex_lock(&mutex2);
sleep(1);
pthread_mutex_lock(&mutex1);
printf("workB....\n");
pthread_mutex_unlock(&mutex1);
pthread_mutex_unlock(&mutex2);
return NULL;
}
int main() {
// 初始化互斥量
pthread_mutex_init(&mutex1, NULL);
pthread_mutex_init(&mutex2, NULL);
// 創(chuàng)建2個子線程
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, workA, NULL);
pthread_create(&tid2, NULL, workB, NULL);
// 回收子線程資源
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
// 釋放互斥量資源
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
return 0;
}
讀寫鎖
文章來源:http://www.zghlxwxcb.cn/news/detail-697182.html
/*
讀寫鎖的類型 pthread_rwlock_t
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
案例:8個線程操作同一個全局變量。
3個線程不定時寫這個全局變量,5個線程不定時的讀這個全局變量
*/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
// 創(chuàng)建一個共享數(shù)據(jù)
int num = 1;
// pthread_mutex_t mutex;
pthread_rwlock_t rwlock;
void * writeNum(void * arg) {
while(1) {
pthread_rwlock_wrlock(&rwlock);
num++;
printf("++write, tid : %ld, num : %d\n", pthread_self(), num); //pthread_self()獲取當前線程的線程ID
pthread_rwlock_unlock(&rwlock);
usleep(100);
}
return NULL;
}
void * readNum(void * arg) {
while(1) {
pthread_rwlock_rdlock(&rwlock);
printf("===read, tid : %ld, num : %d\n", pthread_self(), num);
pthread_rwlock_unlock(&rwlock);
usleep(100);
}
return NULL;
}
int main() {
pthread_rwlock_init(&rwlock, NULL);
// 創(chuàng)建3個寫線程,5個讀線程
pthread_t wtids[3], rtids[5];
for(int i = 0; i < 3; i++) {
pthread_create(&wtids[i], NULL, writeNum, NULL);
}
for(int i = 0; i < 5; i++) {
pthread_create(&rtids[i], NULL, readNum, NULL);
}
// 設(shè)置線程分離
for(int i = 0; i < 3; i++) {
pthread_detach(wtids[i]);
}
for(int i = 0; i < 5; i++) {
pthread_detach(rtids[i]);
}
pthread_exit(NULL);
pthread_rwlock_destroy(&rwlock);
return 0;
}
生產(chǎn)者與消費者模型
文章來源地址http://www.zghlxwxcb.cn/news/detail-697182.html
/*
生產(chǎn)者消費者模型(粗略的版本)
*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
// 創(chuàng)建一個互斥量
pthread_mutex_t mutex;
struct Node{
int num;
struct Node *next;
};
// 頭結(jié)點
struct Node * head = NULL;
void * producer(void * arg) {
// 不斷的創(chuàng)建新的節(jié)點,添加到鏈表中
while(1) {
pthread_mutex_lock(&mutex);
struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->next = head;
head = newNode;
newNode->num = rand() % 1000;
printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
pthread_mutex_unlock(&mutex);
usleep(100);
}
return NULL;
}
void * customer(void * arg) {
while(1) {
pthread_mutex_lock(&mutex);
// 保存頭結(jié)點的指針
struct Node * tmp = head;
// 判斷是否有數(shù)據(jù)
if(head != NULL) {
// 有數(shù)據(jù)
head = head->next;
printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
free(tmp);
pthread_mutex_unlock(&mutex);
usleep(100);
} else {
// 沒有數(shù)據(jù)
pthread_mutex_unlock(&mutex);
}
}
return NULL;
}
int main() {
pthread_mutex_init(&mutex, NULL);
// 創(chuàng)建5個生產(chǎn)者線程,和5個消費者線程
pthread_t ptids[5], ctids[5];
for(int i = 0; i < 5; i++) {
pthread_create(&ptids[i], NULL, producer, NULL);
pthread_create(&ctids[i], NULL, customer, NULL);
}
for(int i = 0; i < 5; i++) {
pthread_detach(ptids[i]);
pthread_detach(ctids[i]);
}
while(1) {
sleep(10);
}
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
return 0;
}
條件變量
/*
條件變量的類型 pthread_cond_t
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
- 等待,調(diào)用了該函數(shù),線程會阻塞。
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);
- 等待多長時間,調(diào)用了這個函數(shù),線程會阻塞,直到指定的時間結(jié)束。
int pthread_cond_signal(pthread_cond_t *cond);
- 喚醒一個或者多個等待的線程
int pthread_cond_broadcast(pthread_cond_t *cond);
- 喚醒所有的等待的線程
*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
// 創(chuàng)建一個互斥量
pthread_mutex_t mutex;
// 創(chuàng)建條件變量
pthread_cond_t cond;
struct Node{
int num;
struct Node *next;
};
// 頭結(jié)點
struct Node * head = NULL;
void * producer(void * arg) {
// 不斷的創(chuàng)建新的節(jié)點,添加到鏈表中
while(1) {
pthread_mutex_lock(&mutex);
struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->next = head;
head = newNode;
newNode->num = rand() % 1000;
printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
// 只要生產(chǎn)了一個,就通知消費者消費
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
usleep(100);
}
return NULL;
}
void * customer(void * arg) {
while(1) {
pthread_mutex_lock(&mutex);
// 保存頭結(jié)點的指針
struct Node * tmp = head;
// 判斷是否有數(shù)據(jù)
if(head != NULL) {
// 有數(shù)據(jù)
head = head->next;
printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
free(tmp);
pthread_mutex_unlock(&mutex);
usleep(100);
} else {
// 沒有數(shù)據(jù),需要等待
// 當這個函數(shù)調(diào)用阻塞的時候,會對互斥鎖進行解鎖,當不阻塞的,繼續(xù)向下執(zhí)行,會重新加鎖。
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
}
return NULL;
}
int main() {
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
// 創(chuàng)建5個生產(chǎn)者線程,和5個消費者線程
pthread_t ptids[5], ctids[5];
for(int i = 0; i < 5; i++) {
pthread_create(&ptids[i], NULL, producer, NULL);
pthread_create(&ctids[i], NULL, customer, NULL);
}
for(int i = 0; i < 5; i++) {
pthread_detach(ptids[i]);
pthread_detach(ctids[i]);
}
while(1) {
sleep(10);
}
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
pthread_exit(NULL);
return 0;
}
信號量
/*
信號量的類型 sem_t
int sem_init(sem_t *sem, int pshared, unsigned int value);
- 初始化信號量
- 參數(shù):
- sem : 信號量變量的地址
- pshared : 0 用在線程間 ,非0 用在進程間
- value : 信號量中的值
int sem_destroy(sem_t *sem);
- 釋放資源
int sem_wait(sem_t *sem);
- 對信號量加鎖,調(diào)用一次對信號量的值-1,如果值為0,就阻塞
int sem_trywait(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
int sem_post(sem_t *sem);
- 對信號量解鎖,調(diào)用一次對信號量的值+1
int sem_getvalue(sem_t *sem, int *sval);
sem_t psem;
sem_t csem;
init(psem, 0, 8);
init(csem, 0, 0);
producer() {
sem_wait(&psem);
sem_post(&csem)
}
customer() {
sem_wait(&csem);
sem_post(&psem)
}
*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
// 創(chuàng)建一個互斥量
pthread_mutex_t mutex;
// 創(chuàng)建兩個信號量
sem_t psem;
sem_t csem;
struct Node{
int num;
struct Node *next;
};
// 頭結(jié)點
struct Node * head = NULL;
void * producer(void * arg) {
// 不斷的創(chuàng)建新的節(jié)點,添加到鏈表中
while(1) {
sem_wait(&psem);
pthread_mutex_lock(&mutex);
struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->next = head;
head = newNode;
newNode->num = rand() % 1000;
printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
pthread_mutex_unlock(&mutex);
sem_post(&csem);
}
return NULL;
}
void * customer(void * arg) {
while(1) {
sem_wait(&csem);
pthread_mutex_lock(&mutex);
// 保存頭結(jié)點的指針
struct Node * tmp = head;
head = head->next;
printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
free(tmp);
pthread_mutex_unlock(&mutex);
sem_post(&psem);
}
return NULL;
}
int main() {
pthread_mutex_init(&mutex, NULL);
sem_init(&psem, 0, 8);
sem_init(&csem, 0, 0);
// 創(chuàng)建5個生產(chǎn)者線程,和5個消費者線程
pthread_t ptids[5], ctids[5];
for(int i = 0; i < 5; i++) {
pthread_create(&ptids[i], NULL, producer, NULL);
pthread_create(&ctids[i], NULL, customer, NULL);
}
for(int i = 0; i < 5; i++) {
pthread_detach(ptids[i]);
pthread_detach(ctids[i]);
}
while(1) {
sleep(10);
}
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
return 0;
}
到了這里,關(guān)于第三章 Linux多線程開發(fā) 線程取消 屬性 同步 互斥鎖 死鎖 讀寫鎖 生產(chǎn)者消費者 信號量的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!