一、線程同步的概念
線程同步是多線程編程中的一個重要概念。在多線程環境中,多個線程同時訪問共享數據時,容易造成數據的不一致性和錯誤。因此,需要對多個線程進行協調與管理,保證它們之間的執行次序、執行結果的可預測性和一致性。
二、多線程4鍾同步方式
常見的線程同步方式有4種,分別是:
- 互斥鎖:採用互斥方式對共享資源進行訪問控制。
- 條件變量:多個線程通過條件變量進行通信和喚醒。
- 自旋鎖:在等待鎖的過程中,不放棄CPU時間,而是進行一定時間的忙等待。
- 信號量:用於控制資源的訪問次數,類似於對某個共享變量的計數器。
三、線程同步的方式有哪些
除了上述提到的常見線程同步方式,還有以下幾種線程同步方式:
- 讀寫鎖:在對共享資源進行讀操作時可以多個線程同時訪問,而在對共享資源進行寫操作時只有一個線程可以訪問。
- 屏障(barrier):用於同步多個線程的執行,使它們在某個點匯合併行執行。
- 原子操作:對共享資源進行操作時,保證操作的原子性,即操作不可中斷。
- 同步隊列:實現多個線程間的消息傳遞,線程在同步隊列中等待消息的到來。
四、加鎖線程是線程同步的方式嗎
加鎖是線程同步的一種實現方式,即通過加鎖控制多個線程訪問共享數據的先後順序,從而實現對數據訪問的同步。在加鎖期間,只有獲得鎖的線程才能訪問共享資源,其他線程需要等待。
五、線程同步和異步
線程同步和異步都是多線程編程中的重要概念。線程同步指多個線程之間的執行順序是固定的,任何一個線程都無法獨立完成任務。而異步則指多個線程之間的執行順序是不確定的,可以獨立完成任務。
六、線程同步的四種方式
再次總結一下線程同步的四種方式:
- 互斥鎖
- 條件變量
- 自旋鎖
- 信號量
七、實現線程同步的方法
實現線程同步的方法有很多,下面介紹幾種比較常用的實現方式:
1、使用互斥鎖進行線程同步
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mutex;
void *threadFunction(void *arg) {
pthread_mutex_lock(&mutex);
printf("Hello from thread %d\n", *(int*)arg);
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t tid[5];
int i, args[5];
pthread_mutex_init(&mutex, NULL);
for (i = 0; i < 5; i++) {
args[i] = i;
pthread_create(&tid[i], NULL, threadFunction, &args[i]);
}
for (i = 0; i < 5; i++) {
pthread_join(tid[i], NULL);
}
pthread_mutex_destroy(&mutex);
return 0;
}
2、使用條件變量進行線程同步
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t condVar;
int count = 0;
void *producer(void *arg) {
int i;
for (i = 0; i < 5; i++) {
pthread_mutex_lock(&mutex);
count++;
printf("producer: produced %d\n", count);
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&condVar);
}
return NULL;
}
void *consumer(void *arg) {
int i;
for (i = 0; i < 5; i++) {
pthread_mutex_lock(&mutex);
while (count == 0) {
pthread_cond_wait(&condVar, &mutex);
}
count--;
printf("consumer: consumed %d\n", count);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
pthread_t tid[2];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condVar, NULL);
pthread_create(&tid[0], NULL, producer, NULL);
pthread_create(&tid[1], NULL, consumer, NULL);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&condVar);
return 0;
}
3、使用原子變量進行線程同步
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdatomic.h>
_Atomic int count = 0;
void *threadFunction(void *arg) {
int i, n = *(int*)arg;
for (i = 0; i < 5; i++) {
atomic_fetch_add(&count, n);
printf("thread %d: count = %d\n", n, count);
}
return NULL;
}
int main() {
pthread_t tid[2];
int args[] = {1, -1};
pthread_create(&tid[0], NULL, threadFunction, &args[0]);
pthread_create(&tid[1], NULL, threadFunction, &args[1]);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
4、使用屏障進行線程同步
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 5
pthread_barrier_t barrier;
void *threadFunction(void *arg) {
int n = *(int*)arg;
printf("thread %d: before barrier\n", n);
pthread_barrier_wait(&barrier);
printf("thread %d: after barrier\n", n);
return NULL;
}
int main() {
pthread_t tid[NUM_THREADS];
int i;
pthread_barrier_init(&barrier, NULL, NUM_THREADS);
for (i = 0; i < NUM_THREADS; i++) {
int *arg = malloc(sizeof(*arg));
*arg = i;
pthread_create(&tid[i], NULL, threadFunction, arg);
}
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(tid[i], NULL);
}
pthread_barrier_destroy(&barrier);
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/312711.html
微信掃一掃
支付寶掃一掃