1) 同步信号量sem
sem_init();
sem_post();
sem_wait()
sem_destroy();
pthread_1: sem_post();
pthread_2: sem_wait();
2) 互斥量mutex
pthread_mutex_init();
pthread_mutex_lock();
pthread_mutex_unlock();
pthread_mutex_destroy();
pthread_1: pthread_mutex_lock();
pthread_mutex_unlock();
pthread_2: pthread_mutex_lock();
pthread_mutex_unlock();
3) 读写锁rwlock
pthread_rwlock_init();
pthread_rwlock_rdlock();
pthread_rwlock_wdlock();
pthread_rwlock_unlock();
pthread_rwlock_destroy();
pthread_1: pthread_rwlock_rdlock();
pthread_rwlock_unlock();
pthread_2: pthread_rwlock_rdlock();
pthread_rwlock_unlock();
......
pthread_n: pthread_rwlock_wdlock();
pthread_rwlock_unlock();
若要实现同步,在实际编程中应该尽可能使用同步信号量而不要使用互斥量。
互斥量会造成轮询死等状态,效率很低。
上述代码执行结果:
# gcc -o sem pthread_sem.c -lpthread
# ./sem
[3076306624] Input some text. Enter 'end' finish
rqrq
[3076303680] Input 4 characters
r23333t4
[3076303680] Input 8 characters
end
[3076306624] Wait for child thread to finish ...
[3076306624] Child pthread joined
sem_init();
sem_post();
sem_wait()
sem_destroy();
pthread_1: sem_post();
pthread_2: sem_wait();
2) 互斥量mutex
pthread_mutex_init();
pthread_mutex_lock();
pthread_mutex_unlock();
pthread_mutex_destroy();
pthread_1: pthread_mutex_lock();
pthread_mutex_unlock();
pthread_2: pthread_mutex_lock();
pthread_mutex_unlock();
3) 读写锁rwlock
pthread_rwlock_init();
pthread_rwlock_rdlock();
pthread_rwlock_wdlock();
pthread_rwlock_unlock();
pthread_rwlock_destroy();
pthread_1: pthread_rwlock_rdlock();
pthread_rwlock_unlock();
pthread_2: pthread_rwlock_rdlock();
pthread_rwlock_unlock();
......
pthread_n: pthread_rwlock_wdlock();
pthread_rwlock_unlock();
若要实现同步,在实际编程中应该尽可能使用同步信号量而不要使用互斥量。
互斥量会造成轮询死等状态,效率很低。
互斥量适用于需要保护的临界区在两线程间的应用没有同步关系,只是互斥。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
sem_t g_sem;
#define SIZE 1024
char g_Word[SIZE] = {0};
void *t_Func(void *arg)
{
pthread_t tid_self;
tid_self = pthread_self();
sem_wait(&g_sem);
while(strncmp("end", g_Word, 3) != 0)
{
printf("[%lu] Input %d characters\r\n", tid_self, (strlen(g_Word)-1));
sem_wait(&g_sem);
}
return NULL;
}
int main()
{
int re;
pthread_t tid, tid_self;
void *t_result;
tid_self = pthread_self();
re = sem_init(&g_sem, 0, 0);
if(0!= re)
{
perror("Semaphore initialization failled");
exit(EXIT_FAILURE);
}
re = pthread_create(&tid, NULL, t_Func, NULL);
if(0 != re)
{
perror("Thread create failled");
exit(EXIT_FAILURE);
}
printf("[%lu] Input some text. Enter 'end' finish \r\n", tid_self);
while(strncmp("end",g_Word, 3) != 0)
{
fgets(g_Word, SIZE, stdin);
sem_post(&g_sem);
}
printf("[%lu] Wait for child thread to finish ...\r\n", tid_self);
re = pthread_join(tid, &t_result);
if(0!= re)
{
perror("Child pthread join failled");
exit(EXIT_FAILURE);
}
printf("[%lu] Child pthread joined \r\n", tid_self);
sem_destroy(&g_sem);
exit(EXIT_SUCCESS);
}
上述代码执行结果:
# gcc -o sem pthread_sem.c -lpthread
# ./sem
[3076306624] Input some text. Enter 'end' finish
rqrq
[3076303680] Input 4 characters
r23333t4
[3076303680] Input 8 characters
end
[3076306624] Wait for child thread to finish ...
[3076306624] Child pthread joined
参考《Linux 程序设计》