多线程中的条件线程简单示例
来源:互联网 发布:linux使用gcc编译器 编辑:程序博客网 时间:2024/06/05 03:14
1.原理:理解的条件线程最常见的用法就是两个线程同时对一个队列进行处理,比如一个线程负责在队列未满的时候插入item,另外一个线程负责在队列非空时取出item,条件线程涉及4个线程相关函数,pthread_mutex_lock、pthread_mutex_unlock、pthread_cond_wait、pthread_cond_signal,其中pthread_mutex_lock负责对线程加锁,以免其他线程同时对共享资源(比如本例中的int cond_num)进行修改,pthread_mutex_unlock对线程解锁,pthread_cond_wait用于阻塞自己这个线程,pthread_cond_signal用于唤醒其他线程,必须遵守pthread_mutex_lock、pthread_cond_wait、pthread_cond_signal、pthread_mutex_unlock的顺序。
2.代码示例
#define __USE_LARGEFILE64
#define _LARGEFILE64_SOURCE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_num_even = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_num_odd = PTHREAD_COND_INITIALIZER;
int cond_num = 1;
void *thread_function_even( )
{
long unsigned int thread_num = pthread_self();
int status = pthread_detach( thread_num ); /*detach to be an independent thread*/
while( 1 )
{
printf("enter thread_function_even,cond_num:%d\n",cond_num);
pthread_mutex_lock( &mutex );
while( 1 == cond_num % 2 )
pthread_cond_wait( &cond_num_even, &mutex );
printf("enter thread_function_even,before deal with cond_num,cond_num:%d\n",cond_num);
cond_num ++; /*cond_num become even,operate it*/
pthread_cond_signal( &cond_num_odd );/*wake odd function to see if it become odd*/
pthread_mutex_unlock( &mutex );
}
}
void * thread_function_odd( )
{
long unsigned int thread_num = pthread_self();
int status = pthread_detach( thread_num ); /*detach to be an independent thread*/
while( 1 )
{
printf("enter thread_function_odd,cond_num:%d\n",cond_num);
pthread_mutex_lock( &mutex );
while( 0 == cond_num % 2 )
pthread_cond_wait( &cond_num_odd, &mutex );
printf("enter thread_function_odd,before deal with cond_num,cond_num:%d\n",cond_num);
cond_num ++;/*cond_num become odd,operate it*/
pthread_cond_signal( &cond_num_even);/*wake even function to see if it become even*/
pthread_mutex_unlock( &mutex );
}
}
int main( void )
{
pthread_t thread;
pthread_create( &thread, NULL, &thread_function_even, NULL );
pthread_create( &thread, NULL, &thread_function_odd, NULL );
while(1); /*avoid thread's quit after main thread quit*/
}
结果:
gcc main.c -lpthread -g
./a.out 后的前几行结果解析
enter thread_function_even,cond_num:1 /*条件数为奇数,直接pthread_cond_wait阻塞跳出该函数直至条件数为偶数为止*/
enter thread_function_odd,cond_num:1 /*条件数符合奇数,跳过阻塞的代码行*/
enter thread_function_odd,before deal with cond_num,cond_num:1 /*处理条件数,条件数变为偶数,将even线程唤醒,因为还未发pthread_cond_wait阻塞自己,继续在odd的while循环中进行*/
enter thread_function_odd,cond_num:2 /*条件数为偶数,直接pthread_cond_wait阻塞跳出该函数直至条件数为奇数为止,因为even已唤醒,跳到even函数处理*/
enter thread_function_even,before deal with cond_num,cond_num:2 /*条件数为偶数,处理条件数*/
...
- 多线程中的条件线程简单示例
- 线程示例:多线程卖票
- linux多线程简单介绍-线程创建、终止、属性设置、多线程同步技术(互斥量、条件变量、信号量)
- 多线程简单示例。。。
- 简单的多线程示例
- 多线程简单示例
- 多线程的简单示例
- 多线程简单示例
- 多线程简单示例
- Java线程同步中的wait()和notify()使用简单示例
- C#简单线程示例
- 线程简单示例
- 线程的简单示例
- 线程池多线程使用示例
- 多线程之线程通信条件Condition
- 多线程之线程通信条件Condition二
- iOS - GCD(多线程)简单示例:
- 一个简单的多线程示例
- 自己动手实现wxpython向导框
- 3808: Neerc2012 Labyrinth of the Minotaur
- 单车故事
- android 三种实现水平向滑动方式(ViewPager、ViewFilpper、ViewFlow)的比较
- Spring 加载bean
- 多线程中的条件线程简单示例
- 黑马程序员_java面向对象基础学习笔记
- 流程图
- 10.缓冲---iOS-Core-Animation-Advanced-Techniques
- [点点搬家]初试mod_perl+apache 之五 Mason
- sqlserver的分组查询功能
- centos下安装tomcat
- postgresql问题总结(远程链接常出现的问题)
- Python学习01---入门