多线程实现相关函数
来源:互联网 发布:cocos2dx游戏源码下载 编辑:程序博客网 时间:2024/05/16 18:16
1 线程的创建
pthread_create 函数用于创建线程
表头文件:#include<pthread.h>
定义函数:int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void*(*start_rtn)(void),void*restrict arg)
函数说明:pthread_t *restrict tidp 要创建线程的线程ID指针 (&tid)
const pthread_attr_t *restrict attr 创建线程时的线程属性(NULL)
void*(*start_rtn)(void) 返回值是void类型的指针函数 (server_request)
void*restrict arg start_rtn的形参 ((void *)clientfd)
返回值: 调用成功后返回0,其他都表示错误,检测到如下情况,pthread_create()将失败并返回相应值
EAGAIN 超出系统限制,线程创建太多
EPERM 调用者没有适当权限设置所需的参数或调度策略
EINVAL 描述 tattr 的值无效
2 线程终止
pthread_exit用于线程终止
定义函数: void pthread_exit(void *retval);
函数说明:使用 pthread_exit退出线程,这是线程的主动行为;由于进程中的多个线程是共享数据段,因此通常在线程退出后,退出线程所占用的资源
并不会随着线程终止而释放,但可以用pthread_join函数来同步并释放
返回值:pthread_exit()调用线程返回值,可由其他函数如pthread_join来检索获取
退出方式有三种: 线程执行函数返回,返回值是线程的退出吗
线程被同一进程的其他线程取消
调用pthread_exit()函数退出
3 线程等待
pthread_join用于线程等待
定义函数:pthread_join(thread_t tid,void **status)
函数说明:参数tid 指要等待的线程ID,线程等待必须在当前进程中,不能在分离进程中,当status部位NULL时,status指向某个位置,在成功返回时,
该位置设为终止线程的退出状态
返回值:成功返回0;不成功返回相应的值
ESRCH 没有找到给定线程ID相应的线程
pthread_create 函数用于创建线程
表头文件:#include<pthread.h>
定义函数:int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void*(*start_rtn)(void),void*restrict arg)
函数说明:pthread_t *restrict tidp 要创建线程的线程ID指针 (&tid)
const pthread_attr_t *restrict attr 创建线程时的线程属性(NULL)
void*(*start_rtn)(void) 返回值是void类型的指针函数 (server_request)
void*restrict arg start_rtn的形参 ((void *)clientfd)
返回值: 调用成功后返回0,其他都表示错误,检测到如下情况,pthread_create()将失败并返回相应值
EAGAIN 超出系统限制,线程创建太多
EPERM 调用者没有适当权限设置所需的参数或调度策略
EINVAL 描述 tattr 的值无效
2 线程终止
pthread_exit用于线程终止
定义函数: void pthread_exit(void *retval);
函数说明:使用 pthread_exit退出线程,这是线程的主动行为;由于进程中的多个线程是共享数据段,因此通常在线程退出后,退出线程所占用的资源
并不会随着线程终止而释放,但可以用pthread_join函数来同步并释放
返回值:pthread_exit()调用线程返回值,可由其他函数如pthread_join来检索获取
退出方式有三种: 线程执行函数返回,返回值是线程的退出吗
线程被同一进程的其他线程取消
调用pthread_exit()函数退出
3 线程等待
pthread_join用于线程等待
定义函数:pthread_join(thread_t tid,void **status)
函数说明:参数tid 指要等待的线程ID,线程等待必须在当前进程中,不能在分离进程中,当status部位NULL时,status指向某个位置,在成功返回时,
该位置设为终止线程的退出状态
返回值:成功返回0;不成功返回相应的值
ESRCH 没有找到给定线程ID相应的线程
EDEADLK 出现死锁
EINVAL 给定线程ID对应线程为分离线程
/* 创建两个线程,实现每隔一秒实现一次打印 */#include<stdio.h>#include<pthread.h>void *mythread1(void){int i;for (i=0; i<100; i++){printf("this is first pthread\n");sleep(1);}}void *mypthread2 (void){int i;for (i=0; i<100; i++){printf("this is secend pthread\n");sleep(1);}}int main(){int i = 0, ret = 0; pthread_t tid1,tid2;/*创建线程1*/ret = pthread_create(&tid1, NULL, (void*)mypthread1, NULL)if (ret){printf("create pthread error!\n");return 1;}/*创建线程2*/ret = pthread_create(&tid1, NULL, (void*)mypthread2, NULL)if (ret){printf("create pthread error!\n");return 1;}pthread_join(tid1,NULL);pthread_join(tid2,NULl);return 0;}
阅读全文
0 0
- 多线程实现相关函数
- Windows下多线程相关函数
- 多线程下载实现函数
- win32多线程的相关函数使用总结
- 多线程同步的条件变量相关函数
- str相关的函数实现
- writel底层函数实现相关
- 字符串相关函数的实现
- 多线程相关
- 多线程相关
- 多线程相关
- 多线程相关
- 多线程 相关
- 多线程相关
- 多线程相关
- 多线程相关
- 多线程相关
- 多线程相关
- VS下scanf&gets的本质区别
- Git学习
- 卡特兰数的性质
- 《锋利的Jquery》笔记(一)
- Linux 批量添加用户
- 多线程实现相关函数
- 子类的构造,析构和拷贝
- synchronized和java.util.concurrent.locks.Lock的异同?
- J2EE学习篇之--JQuery技术详解
- zpeto源码之核心代码一
- linux根据端口号查询项目路径
- MATLAB常用命令说明(1)
- 加和求不同的组合方式数目(dp)
- Ubuntu下安装Ctags