Linux下C编程:线程操作
来源:互联网 发布:彩票大数据分析 编辑:程序博客网 时间:2024/05/22 19:12
Linux下多线程开发一般是采用pthread库,因此编译项目的时候需要链接pthread库才可以。
创建线程很简单,int pthread_create(pthread_t*thread, pthread_attr_t *attr, ... );//
void pthread_exit(); //结束线程
int pthread_join(pthread th, void **thread_return); //挂起当前进程直至指定线程终止,这个可以用以进行一些简单的线程同步。
多线程开发中最重要的问题就是线程同步了,Linux按照POSIX标准提供了两种同步机制,互斥锁(mutex)和条件变量(condition variable),实际上这两中机制可以配合使用。
下面这个是书上给的一个利用互斥锁和同步变量实现的生产者/消费者例子:
/* ============================================================================ Name : threadmemo.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <pthread.h>#define BUFFER_SIZE 4#define OVER (-1)struct producers{int buffer[BUFFER_SIZE];pthread_mutex_tlock;intreadpos, writepos;pthread_cond_tnotempty;pthread_cond_tnotfull;};void init(struct producers *b){pthread_mutex_init(&b->lock,NULL);pthread_cond_init(&b->notempty,NULL);pthread_cond_init(&b->notfull,NULL);b->readpos=0;b->writepos=0;}void put(struct producers *b, int data){pthread_mutex_lock(&b->lock);while((b->writepos+1)%BUFFER_SIZE==b->readpos){pthread_cond_wait(&b->notfull,&b->lock);}b->buffer[b->writepos]=data;b->writepos++;if(b->writepos>=BUFFER_SIZE) b->writepos=0;pthread_cond_signal(&b->notempty);pthread_mutex_unlock(&b->lock);}int get(struct producers *b){int data;pthread_mutex_lock(&b->lock);while(b->writepos==b->readpos){pthread_cond_wait(&b->notempty,&b->lock);}data=b->buffer[b->readpos];b->readpos++;if(b->readpos>=BUFFER_SIZE) b->readpos=0;pthread_cond_signal(&b->notfull);pthread_mutex_unlock(&b->lock);return data;}struct producers buffer;void *producer(void *data){int n;for(n=0;n<10;n++){printf("Producer : %d-->\n",n);put(&buffer,n);}put(&buffer,OVER);return NULL;}void *consumer(void *data){int d;while(1){d=get(&buffer);if(d==OVER) break;printf("Consumer: --> %d\n",d);}return NULL;}int main(){pthread_t tha,thb;void *retval;init(&buffer);pthread_create(&tha,NULL,producer,0);pthread_create(&thb,NULL,consumer,0);pthread_join(tha,&retval);pthread_join(thb,&retval);return 0;}现在就是有点疑惑,查阅了一些资料,有人说pthread是用户态线程库,但是又有人说不是,说和内核线程是一一对应的,不知道哪种说法准确。
0 0
- Linux下C语言编程--线程操作
- Linux下C语言编程--线程操作
- Linux下C编程:线程操作
- linux操作系统下c语言编程入门 -- (7)线程操作
- linux操作系统下c语言编程入门--线程操作
- linux操作系统下c语言编程入门--线程操作
- Linux系统下C语言编程 线程操作入门常识
- Linux下的C语言编程——线程编程基本操作
- Linux下的C语言编程——线程编程基本操作
- Linux下的C语言编程——线程编程基本操作
- Linux下的C语言编程——线程编程基本操作
- linux下使用c进行线程操作
- Linux下c语言线程编程问题
- 极客学院课程Linux下 C 编程——线程操作
- Linux C编程--线程操作2--线程同步详解
- Linux C编程--线程操作3--线程属性解析
- Linux C编程--线程操作线程属性解析
- Linux下C编程操作MySql数据库
- PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re
- 编程珠玑:插入排序的最优探索,以及快排
- 133. Which three statements are true? (Choose three.)
- win7 64位免安装版mysql配置
- Java反射得到属性的值和设置属性的值
- Linux下C编程:线程操作
- http://www.google.com/ncr 谷歌无限制搜索方法,用过都说好
- 富文本编辑器添加的图片手机端不能自适应问题
- 提高效率必须改掉的7种习惯
- jsp链接路径的学习
- Day2 - F - Never Wait forWeights
- 黑马程序员 学习日记(四)
- I深搜
- Swift实战(1)-helloworld