UNIX 网络编程之线程
来源:互联网 发布:mac的office软件叫啥 编辑:程序博客网 时间:2024/06/11 13:09
概述:
实现并发服务器一般都是父进程accept一个连接,然后fork一个子进程,该子进程处理与该连接对端的客户之间的通信。但是fork是昂贵,耗资源和时间。而线程是轻量级线程,它的创建比进程的创建块10-100倍。在同一进程内除了共享全局变量外还共享:
大多数数据;进程指令; 打开的文件; 信号处理函数信号处置; 当前工作目录;用户ID和组ID
不过每个线程有各自的资源:‘
线程ID; 寄存器集合了栈了errno; 信号掩码; 优先级
基本线程函数:创建和终止
pthread_create函数
#include <pthread.h>
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void *), void *arg);
一个进程的每个线程都由一个线程ID标识。每个线程有很多属性,比如优先级大小,初始栈大小,是否应该成为一个守护线程等等
pthread_join函数
#include <pthread.h>
int pthread_join(pthread_t *tid, void **status);
该函数类似与waitpid
pthread_self函数
#include <pthread.h>
int pthread_self(void);
每个线程使用pthread_self获得自身的线程ID
pthread_detach函数
#include <pthread.h>
int pthread_detach(pthread_t tid);
一个线程或者是可汇合的,或者是脱离的。当一个可汇合的线程终止时,它的线程ID和退出状态将留存到另一个线程对它调用pthread_join。脱离的线程像守护线程,当他们终止时,所有相关资源都被释放.
pthread_exit函数
#include <pthread.h>
int pthread_exit(void *status);
结束一个线程
互斥锁的使用
多线程程序的经典问题:多个线程同时修改一个共享变量(如全局变量)
#include <pthread.h>#include <stdio.h>int counter;void *doit(void*);int main(int argc, char **argv){pthread_t tidA, tidB;pthread_create(&tidA, NULL, &doit, NULL);pthread_create(&tidB, NULL, &doit, NULL);pthread_join(tidA, NULL);pthread_join(tidB, NULL);return 0;}void *doit(void * arg){int i, val;for(i=0; i<10; i++){val = counter;printf("counter is %d\n", val+1);counter = val+1;}return NULL;}上面程序的运行结果并不是我们想要的结果,因为线程的运行是并发运行的,也就是说counter值的修改的结果是不定的,以下为运行结果
所以我们应该引入同步机制,首先使用互斥量实现
#include <pthread.h>#include <stdio.h>int counter;pthread_mutex_t counter_mutex;void *doit(void*);int main(int argc, char **argv){pthread_t tidA, tidB;pthread_create(&tidA, NULL, &doit, NULL);pthread_create(&tidB, NULL, &doit, NULL);pthread_join(tidA, NULL);pthread_join(tidB, NULL);return 0;}void *doit(void * arg){int i, val;for(i=0; i<10; i++){pthread_mutex_lock(&counter_mutex);val = counter;printf("counter is %d\n", val+1);counter = val+1;pthread_mutex_unlock(&counter_mutex);}return NULL;}
使用在对counter值进行修改之前进行上锁操作,修改之后,进行解锁操作
- UNIX 网络编程之线程
- UNIX网络编程之线程浅析
- unix编程之线程 笔记
- unix网络编程之tcp
- unix网络编程之udp
- UNIX网络编程之管道
- UNIX网络编程之epoll
- 线程同步-互斥锁-unix网络编程
- 线程同步-条件变量-unix网络编程
- UNIX网络编程----线程(二十六)
- 【UNIX网络编程学习】线程特定数据
- 《UNIX网络编程 卷1》 笔记: 线程
- 网络编程一<线程间通信的方法之UNIX域套接字>
- UNIX/LINUX编程学习之线程pthread
- UNIX环境高级编程之线程
- UNIX环境高级编程笔记之线程
- Unix网络编程--基于线程的并发编程(1)
- unix网络编程之环境配置
- 关于mysql权限不够的解决方法
- DHTML【10】--Javascript
- 微信可能会颠覆快递的运营模式
- LeetCode - Maximum Subarray
- LINQ体验(4)——LINQ简介和LINQ to SQL语句之Where
- UNIX 网络编程之线程
- Fork/Join框架之双端队列
- 快速排序(C++)
- oracle常用函数
- 64位平台C/C++开发注意事项
- 优化PHP代码的40条建议
- 创业者的11个人生定律
- int,long,long long,unsigned int,unsigned long,unsigned long long范围
- 【Java】this与static关键字