多线程编程2

//信号量同步
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<semaphore.h> //信号量函数定义
#include<string.h>
void *thread_fun(void *arg);
sem_t bin_sem;
#define WORKSIZE 1024
char workarea[WORKSIZE];

int main(){
int res;
pthread_t thread;
void*thread_result;
res=sem_init(&bin_sem,0,0); //信号量初始化 指定一个sem 类型为当前进程的局部变量 初始值为0
//在创建线程之前 初始化信号量
if(res!=0){
perror(“error\n”);
exit(EXIT_FAILURE);
}
res=pthread_create(&thread,NULL,thread_fun,NULL);
if(res!=0){
perror(“error\n”);
exit(EXIT_FAILURE);
}
printf(“input some text.enter 'end’to finish\n”);
while(strncmp(“end”,workarea,3)!=0){
fgets(workarea,WORKSIZE,stdin); //标准输入流
//fgets(workarea,WORKSIZE,1);
sem_post(&bin_sem); //信号值+1
}
printf("\nwaiting for thread to finish\n");
res=pthread_join(thread,&thread_result);
if(res!=0){
perror(“error!\n”);
exit(EXIT_FAILURE);
}
printf(“thread joined\n”);
sem_destroy(&bin_sem); //销毁信号量
return 0;
}
void *thread_fun(void * arg){
sem_wait(&bin_sem); // 信号量-1
while(strncmp(“end”,workarea,3)!=0){
printf(“you put %d characters\n”,strlen(workarea)-1);
sem_wait(&bin_sem);
}
pthread_exit(NULL);
}
初始化信号量时,设置为0——线程函数在启动时,sem_wait()调用就会阻塞,并且等待信号量变成非零值。

//互斥量实现进程同步
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<string.h>
void* thread_fun(void*arg);
pthread_mutex_t work_mutex;
#define WORKSIZE 1024
char workarea[WORKSIZE];
int time_to_exit;

int main(){
int res;
pthread_t thread;
voidthread_result;
res=pthread_mutex_init(&work_mutex,NULL); //NULL为互斥量的属性 表示默认行为
if(res!=0){
perror(“error\n”);
exit(EXIT_FAILURE);
}
res=pthread_create(&thread,NULL,thread_fun,NULL);
if(res!=0){
perror(“error\n”);
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&work_mutex);
printf(“input some text 'end’to finish\n”);
while(!time_to_exit){
fgets(workarea,WORKSIZE,stdin);
pthread_mutex_unlock(&work_mutex);
while(1){
pthread_mutex_lock(&work_mutex);
if(workarea[0]!=’\0’){
pthread_mutex_unlock(&work_mutex);
sleep(1);
}
else{
break;
}
}
}
pthread_mutex_unlock(&work_mutex);
printf("\nwaiting for thread to finish\n");
res=pthread_join(thread,&thread_result);
if(res!=0){
perror(“error\n”);
exit(EXIT_FAILURE);
}
printf(“thread joined\n”);
pthread_mutex_destroy(&work_mutex);
return 0;
}
void
thread_fun(void*arg){
sleep(1);
pthread_mutex_lock(&work_mutex);
while(strncmp(“end”,workarea,3)!=0){
printf(“you put %d characters\n”,strlen(workarea)-1);
workarea[0]=’\0’;
pthread_mutex_unlock(&work_mutex);
sleep(1);
pthread_mutex_lock(&work_mutex);
while(workarea[0]==’\0’){
pthread_mutex_unlock(&work_mutex);
sleep(1);
pthread_mutex_lock(&work_mutex);
}
}
time_to_exit=1;
workarea[0]=’\0’;
pthread_mutex_unlock(&work_mutex);
pthread_exit(NULL);
}
使用互斥量实现多线程的同步访问——允许程序员锁住某个对象,使其每次只被一个线程访问。
为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作后解锁它。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值