在Debian进程中,实现并发控制主要依赖于Linux内核提供的同步原语和机制。以下是一些常用的方法:
互斥锁是一种同步机制,用于保护共享资源,确保在同一时间只有一个进程可以访问该资源。
#include <pthread.h>
pthread_mutex_t mutex;
int shared_resource;
void* thread_func(void* arg) {
pthread_mutex_lock(&mutex);
// 访问共享资源
shared_resource++;
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread1, NULL, thread_func, NULL);
pthread_create(&thread2, NULL, thread_func, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
信号量是一种更高级的同步机制,可以用于控制对一组资源的访问。
#include <semaphore.h>
#include <pthread.h>
sem_t semaphore;
int shared_resource;
void* thread_func(void* arg) {
sem_wait(&semaphore);
// 访问共享资源
shared_resource++;
sem_post(&semaphore);
return NULL;
}
int main() {
pthread_t thread1, thread2;
sem_init(&semaphore, 0, 1); // 初始化信号量,初始值为1
pthread_create(&thread1, NULL, thread_func, NULL);
pthread_create(&thread2, NULL, thread_func, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
sem_destroy(&semaphore);
return 0;
}
条件变量用于线程间的等待和通知机制,常与互斥锁一起使用。
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int ready = 0;
void* producer(void* arg) {
pthread_mutex_lock(&mutex);
// 生产数据
ready = 1;
pthread_cond_signal(&cond); // 通知消费者
pthread_mutex_unlock(&mutex);
return NULL;
}
void* consumer(void* arg) {
pthread_mutex_lock(&mutex);
while (!ready) {
pthread_cond_wait(&cond, &mutex); // 等待生产者通知
}
// 消费数据
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
读写锁允许多个读取者同时访问共享资源,但写入者独占访问。
#include <pthread.h>
pthread_rwlock_t rwlock;
int shared_resource;
void* reader(void* arg) {
pthread_rwlock_rdlock(&rwlock);
// 读取共享资源
pthread_rwlock_unlock(&rwlock);
return NULL;
}
void* writer(void* arg) {
pthread_rwlock_wrlock(&rwlock);
// 写入共享资源
pthread_rwlock_unlock(&rwlock);
return NULL;
}
int main() {
pthread_t reader_threads[5], writer_threads[2];
pthread_rwlock_init(&rwlock, NULL);
for (int i = 0; i < 5; i++) {
pthread_create(&reader_threads[i], NULL, reader, NULL);
}
for (int i = 0; i < 2; i++) {
pthread_create(&writer_threads[i], NULL, writer, NULL);
}
for (int i = 0; i < 5; i++) {
pthread_join(reader_threads[i], NULL);
}
for (int i = 0; i < 2; i++) {
pthread_join(writer_threads[i], NULL);
}
pthread_rwlock_destroy(&rwlock);
return 0;
}
原子操作是不可分割的操作,可以确保在多线程环境中对共享变量的操作是线程安全的。
#include <stdatomic.h>
#include <pthread.h>
atomic_int shared_resource;
void* thread_func(void* arg) {
atomic_fetch_add(&shared_resource, 1);
return NULL;
}
int main() {
pthread_t threads[10];
atomic_init(&shared_resource, 0);
for (int i = 0; i < 10; i++) {
pthread_create(&threads[i], NULL, thread_func, NULL);
}
for (int i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
printf("Shared resource: %d\n", atomic_load(&shared_resource));
return 0;
}
这些方法可以根据具体的应用场景和需求选择使用,以实现有效的并发控制。