线程与信号量

实例1

/*file1*/
//file1实现了1个信号量,对两个线程的同步.
#include <semaphore.h>

#include <pthread.h>
#include <stdio.h>

pthread_t   pthid1,pthid2;
sem_t       sem;

static void * fun1(void *arg){
    sem_wait(&sem);
    puts("second");
    return NULL;
}

static void * fun2(void *arg){
    puts("fist");
    sem_post(&sem);
    return NULL;
}

int main(int argc, const char *argv[])
{
    sem_init(&sem, 0, 0);

    if(0 != pthread_create(&pthid1,NULL,fun1,NULL)){
        perror("pthid1");
        return -1;
    }
    if(0 != pthread_create(&pthid2,NULL,fun2,NULL)){
        perror("pthid2");
        return -1;
    }

    pthread_join(pthid1,NULL);
    pthread_join(pthid2,NULL);

    return 0;
}

实例2

/*file2*/
//file2 实现了2个信号量,对两个线程的同步
#include <semaphore.h>

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

pthread_t   pthid1,pthid2;
sem_t       sem1;
sem_t       sem2;

int i = 1000;

static void * fun1(void *arg){
    while(1){
        sem_wait(&sem2);

        printf("thread1 --- %d\n",--i);
        usleep(100000);

        sem_post(&sem1);
    }
    return NULL;
}

static void * fun2(void *arg){
    while(1){
        sem_wait(&sem1);

        printf("thread2 --- %d\n",--i);
        usleep(100000);

        sem_post(&sem2);
    }
    return NULL;
}


int main(int argc, const char *argv[])
{

    sem_init(&sem1, 0, 1);
    sem_init(&sem2, 0, 0);

    if(0 != pthread_create(&pthid1,NULL,fun1,NULL)){
        perror("pthid1");
        return -1;
    }
    if(0 != pthread_create(&pthid2,NULL,fun2,NULL)){
        perror("pthid2");
        return -1;
    }

    pthread_join(pthid1,NULL);
    pthread_join(pthid2,NULL);

    return 0;
}

实例3

/*file3*/
//file3 实现了一个信号量,对两个线程的互斥
#include <semaphore.h>

#include <pthread.h>
#include <stdio.h>

pthread_t   pthid1,pthid2;
sem_t       sem;

static void * fun1(void *arg){
    sem_wait(&sem);
    puts("second");
    sem_post(&sem);
    return NULL;
}

static void * fun2(void *arg){
    sem_wait(&sem);
    puts("fist");
    sem_post(&sem);
    return NULL;
}

int main(int argc, const char *argv[])
{
    sem_init(&sem, 0, 1);

    if(0 != pthread_create(&pthid1,NULL,fun1,NULL)){
        perror("pthid1");
        return -1;
    }
    if(0 != pthread_create(&pthid2,NULL,fun2,NULL)){
        perror("pthid2");
        return -1;
    }

    pthread_join(pthid1,NULL);
    pthread_join(pthid2,NULL);

    return 0;
}

重点函数

  • sem_init
int sem_init(sem_t *sem, int pshared, unsigned int value);

1/功能
初始化信号量对应的资源数目

2/参数
参数1 为 信号量指针
参数2 为 是否共享,0表示共享在线程中,进程中,非0表示只共享在进程中
参数3 为 资源的数量

3/返回值
返回值 ,成功为0,失败为-1
  • sem_wait

1/功能

抢占资源

如果参数指向的资源为0,则阻塞
如果参数指向的资源大于0,则资源减1,函数返回

  • sem_post
1/功能

释放资源

资源加1,返回.
注意: 资源不可能为负数.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值