Chinaunix首页 | 论坛 | 博客
  • 博客访问: 163047
  • 博文数量: 25
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 319
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-12 21:27
文章分类

全部博文(25)

文章存档

2015年(2)

2014年(1)

2013年(22)

我的朋友

分类: C/C++

2013-06-06 11:14:24




(一)信号量

(二)互斥量


(一)信号量

信号量是一个特殊类型的变量,可对其进行增加或者减少操作,对其的操作是原子操作。

信号量一般分二进制信号量和计数信号量。二进制信号量其实就是计数信号量的一个特例。

信号量可以对某一段代码进行保护,允许同一时刻最多指定个线程使用。而其二进制信号量和互斥量可以说是一样的。

信号量操作函数:

#include

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

Int sem_wait(sem_t *sem);

int sem_trywait(sem_t *sem);

Int sem_post(sem_t *sem);

Int sem_destroy(sem_t *sem);

Sem_init:创建信号量并初始化其值。第一个参数为信号量对象,第二个参数表示是否允许其他进程共享该信号量,目前只能用0,第三个参数是信号量的值。

Sem_wait:以原子操作方式将信号量加一。

sem_trywait:以原子操作方式尝试将信号量加一。

Sem_post:以原子操作方式将信号量减一。

Sem_destroy:清理信号量所拥有的资源。


(二)互斥量

互斥量也是一特殊类型的变量,它如同一把锁,它允许开发人员锁住某个对象,使其在同一时刻只有一个线程可以访问之。

互斥量操作函数:

#include 

Int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *nutexattr);

Int pthread_mutex_lock(pthread_mutex_t *mutex);

Int pthread_mutex_trylock(pthread_mutex_t *mutex);

Int pthread_mutex_unlock(pthread_mutex_t *mutex);

Int pthread_mutex_destroy(pthread_mutex_t *mutex);

pthread_mutex_init:创建互斥量,并设置互斥量的属性,NULL即可。

pthread_mutex_lock:以原子操作方式给互斥量上锁。

pthread_mutex_trylock:以原子操作方式尝试给互斥量加锁

pthread_mutex_unlock:以原子操作方式给互斥量解锁。

pthread_mutex_destroy:清除互斥量。



代码示例:

(一)信号量

#include 

#include 

#include 

#include 

#include 

#include 


void *thread_function(void *arg);

sem_t bin_sem;


#define WORK_SIZE 1024

char work_area[WORK_SIZE];


int main() {

    int res;

    pthread_t a_thread;

    void *thread_result;


    res = sem_init(&bin_sem, 0, 0);

    if (res != 0) {

        perror("Semaphore initialization failed");

        exit(EXIT_FAILURE);

    }

    res = pthread_create(&a_thread, NULL, thread_function, NULL);

    if (res != 0) {

        perror("Thread creation failed");

        exit(EXIT_FAILURE);

    }

    printf("Input some text. Enter 'end' to finish\n");

    while(strncmp("end", work_area, 3) != 0) {

        fgets(work_area, WORK_SIZE, stdin);

        sem_post(&bin_sem);

    }

    printf("\nWaiting for thread to finish...\n");

    res = pthread_join(a_thread, &thread_result);

    if (res != 0) {

        perror("Thread join failed");

        exit(EXIT_FAILURE);

    }

    printf("Thread joined\n");

    sem_destroy(&bin_sem);

    exit(EXIT_SUCCESS);

}


void *thread_function(void *arg) {

    sem_wait(&bin_sem);

    while(strncmp("end", work_area, 3) != 0) {

        printf("You input %d characters\n", strlen(work_area) -1);

        sem_wait(&bin_sem);

    }

    pthread_exit(NULL);

}

运行结果:



(二)互斥量

#include 

#include 

#include 

#include 

#include 

#include 


void *thread_function(void *arg);

pthread_mutex_t work_mutex; /* protects both work_area and time_to_exit */


#define WORK_SIZE 1024

char work_area[WORK_SIZE];

int time_to_exit = 0;


int main() {

    int res;

    pthread_t a_thread;

    void *thread_result;

    res = pthread_mutex_init(&work_mutex, NULL);

    if (res != 0) {

        perror("Mutex initialization failed");

        exit(EXIT_FAILURE);

    }

    res = pthread_create(&a_thread, NULL, thread_function, NULL);

    if (res != 0) {

        perror("Thread creation failed");

        exit(EXIT_FAILURE);

    }

    pthread_mutex_lock(&work_mutex);

    printf("Input some text. Enter 'end' to finish\n");

    while(!time_to_exit) {

        fgets(work_area, WORK_SIZE, stdin);

        pthread_mutex_unlock(&work_mutex);

        while(1) {

            pthread_mutex_lock(&work_mutex);

            if (work_area[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(a_thread, &thread_result);

    if (res != 0) {

        perror("Thread join failed");

        exit(EXIT_FAILURE);

    }

    printf("Thread joined\n");

    pthread_mutex_destroy(&work_mutex);

    exit(EXIT_SUCCESS);

}


void *thread_function(void *arg) {

    sleep(1);

    pthread_mutex_lock(&work_mutex);

    while(strncmp("end", work_area, 3) != 0) {

        printf("You input %d characters\n", strlen(work_area) -1);

        work_area[0] = '\0';

        pthread_mutex_unlock(&work_mutex);

        sleep(1);

        pthread_mutex_lock(&work_mutex);

        while (work_area[0] == '\0' ) {

            pthread_mutex_unlock(&work_mutex);

            sleep(1);

            pthread_mutex_lock(&work_mutex);

        }

    }

    time_to_exit = 1;

    work_area[0] = '\0';

    pthread_mutex_unlock(&work_mutex);

    pthread_exit(0);

}

运行结果:


阅读(3871) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~