线程间同步机制
互斥锁通信机制
互斥以排他方式防止共享数据被并发修改。
(1)在访问该资源前,首先申请该互斥锁,如果该互斥处于开锁状态,则申请到该锁对象,
并立即占有该锁,以防止其他线程访问该资源。如果该互斥锁处于锁定状态,默认阻塞等待。
(2)只有锁定该互斥锁的进程才能释放该互斥锁,其他线程的释放操作无效。
初始化锁
静态初始化互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
动态初始化互斥锁
int pthread_mutex_init(pthread_mutex_t *restrict mutex,
const pthread_mutexattr_t *restrict attr);
销毁互斥锁
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<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
void *tfun(void *arg);
pthread_mutex_t gmutex;
#define BUF 1024
char gdata[BUF];
int gtime = 0;
int main(int argc,char*argv[])
{
int res;
pthread_t th1;
void *thret;
res = pthread_mutex_init(&gmutex,0);
if(res != 0)
{
printf("mutex initialization failed\n");
exit(-1);
}
res = pthread_create(&th1,0,tfun,0);
if(res != 0)
{
printf("thread create failed\n");
exit(-1);
}
pthread_mutex_lock(&gmutex);
printf("input some text,enter 'exit' to finished\n");
while(!gtime)
{
fgets(gdata,BUF,stdin);
pthread_mutex_unlock(&gmutex);
while(1)
{
pthread_mutex_lock(&gmutex);
if(gdata[0] != '\0')
{
pthread_mutex_unlock(&gmutex);
sleep(1);
}
else
break;
}
}
pthread_mutex_unlock(&gmutex);
printf("\n waiting for thread to finish ...\n");
res = pthread_join(th1,&thret);
if(res != 0)
{
printf("thread join failed\n");
exit(-1);
}
pthread_mutex_destroy(&gmutex);
exit(0);
}
void *tfun(void * arg)
{
sleep(1);
pthread_mutex_lock(&gmutex);
while(strncmp("end",gdata,3) != 0)
{
printf("you input %d bytes %s\n",strlen(gdata)-1,gdata);
gdata[0] = '\0';
pthread_mutex_unlock(&gmutex);
sleep(1);
pthread_mutex_lock(&gmutex);
while(gdata[0] == '\0')
{
pthread_mutex_unlock(&gmutex);
sleep(1);
pthread_mutex_lock(&gmutex);
}
}
gtime = 1;
gdata[0] = '\0';
pthread_mutex_unlock(&gmutex);
pthread_exit(0);
}
互斥锁通信机制
互斥以排他方式防止共享数据被并发修改。
(1)在访问该资源前,首先申请该互斥锁,如果该互斥处于开锁状态,则申请到该锁对象,
并立即占有该锁,以防止其他线程访问该资源。如果该互斥锁处于锁定状态,默认阻塞等待。
(2)只有锁定该互斥锁的进程才能释放该互斥锁,其他线程的释放操作无效。
初始化锁
静态初始化互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
动态初始化互斥锁
int pthread_mutex_init(pthread_mutex_t *restrict mutex,
const pthread_mutexattr_t *restrict attr);
销毁互斥锁
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<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>
void *tfun(void *arg);
pthread_mutex_t gmutex;
#define BUF 1024
char gdata[BUF];
int gtime = 0;
int main(int argc,char*argv[])
{
int res;
pthread_t th1;
void *thret;
res = pthread_mutex_init(&gmutex,0);
if(res != 0)
{
printf("mutex initialization failed\n");
exit(-1);
}
res = pthread_create(&th1,0,tfun,0);
if(res != 0)
{
printf("thread create failed\n");
exit(-1);
}
pthread_mutex_lock(&gmutex);
printf("input some text,enter 'exit' to finished\n");
while(!gtime)
{
fgets(gdata,BUF,stdin);
pthread_mutex_unlock(&gmutex);
while(1)
{
pthread_mutex_lock(&gmutex);
if(gdata[0] != '\0')
{
pthread_mutex_unlock(&gmutex);
sleep(1);
}
else
break;
}
}
pthread_mutex_unlock(&gmutex);
printf("\n waiting for thread to finish ...\n");
res = pthread_join(th1,&thret);
if(res != 0)
{
printf("thread join failed\n");
exit(-1);
}
pthread_mutex_destroy(&gmutex);
exit(0);
}
void *tfun(void * arg)
{
sleep(1);
pthread_mutex_lock(&gmutex);
while(strncmp("end",gdata,3) != 0)
{
printf("you input %d bytes %s\n",strlen(gdata)-1,gdata);
gdata[0] = '\0';
pthread_mutex_unlock(&gmutex);
sleep(1);
pthread_mutex_lock(&gmutex);
while(gdata[0] == '\0')
{
pthread_mutex_unlock(&gmutex);
sleep(1);
pthread_mutex_lock(&gmutex);
}
}
gtime = 1;
gdata[0] = '\0';
pthread_mutex_unlock(&gmutex);
pthread_exit(0);
}