【linux草鞋应用编程系列】_4_ 应用程序多线程
一、应用程序多线程
SYNOPSIS
#include <pthread.h> int pthread_create(pthread_t *restrict thread, //标志线程的变量地址
const pthread_attr_t *restrict attr, //创建的线程的属性
void *(*start_routine)(void*), //线程回调处理函数
void *restrict arg //传递给线程回调函数的参数
);
SYNOPSIS
#include <pthread.h> void pthread_exit(void *value_ptr //线程回调函数的返回值, 相当于线程回调函数的 return 的返回值。
);
SYNOPSIS
#include <pthread.h> int pthread_join(pthread_t thread, //调用进程需要等待的线程标志变量
void **value_ptr //输出参数,如果整个参数不为空,那么就指向线程回调函数的返回值
);
SYNOPSIS
#include <pthread.h> int pthread_detach(pthread_t thread //要自动释放系统资源的线程的标志
);
SYNOPSIS
#include <pthread.h> pthread_t pthread_self(void);
SYNOPSIS
#include <pthread.h> int pthread_cancel(pthread_t thread //接受请求的线程的标志
);
SYNOPSIS
#include <pthread.h>
//pthread_setcancelstate用来设置线程的取消状态,禁止取消或者使能取消, 默认为使能的
int pthread_setcancelstate(int state, //线程要设置的状态
int *oldstate //线程原来的状态
);
//pthread_setcanceltype函数用来设置线程的取消类型,
int pthread_setcanceltype(int type,
int *oldtype
);
//pthread_testcancel函数用来设置线程的撤销点
void pthread_testcancel(void);
#include <stdio.h>
#include <pthread.h> void* thread_test(void* argv)
{
int i;
for(i=;i<;i++)
{
printf("in thread_test the i =%d.\n",i);
}
return NULL;
} int main(void)
{
int i;
pthread_t pthread; //创建一个新的线程
pthread_create(&pthread,NULL,thread_test,NULL); for(i=;i<;i++)
{
printf("in main function the i=%d.\n",i);
}
return ;
}
[root@localhost thread]# ./a.out
in main function the i=0.
in main function the i=1.
in main function the i=2.
in main function the i=3.
#include <stdio.h>
#include <pthread.h> void* thread_test(void* argv)
{
int i;
for(i=;i<;i++)
{
printf("in thread_test the i =%d.\n",i);
} return NULL;
} int main(void)
{
int i;
int* thread_val=NULL;
pthread_t pthread; //创建一个新的线程
pthread_create(&pthread,NULL,thread_test,NULL);
pthread_join(pthread,(void *)&thread_val); //增加线程资源释放方式 for(i=;i<;i++)
{
printf("in main function the i=%d.\n",i);
}
return ;
}
[root@localhost thread]# gcc -lpthread main.c
[root@localhost thread]# ./a.out
in thread_test the i =.
in thread_test the i =.
in thread_test the i =.
in thread_test the i =.
in main function the i=.
in main function the i=.
in main function the i=.
in main function the i=.
//创建一个新的线程
pthread_create(&pthread,NULL,thread_test,NULL);
//这里的语句放到下面了
for(i=;i<;i++)
{
printf("in main function the i=%d.\n",i);
pthread_join(pthread,(void *)&thread_val); //将设定线程资源释放方式函数放到这里
}
[root@localhost thread]# gcc -lpthread main.c
[root@localhost thread]# ./a.out
in main function the i=.
in thread_test the i =.
in thread_test the i =.
in thread_test the i =.
in thread_test the i =.
in main function the i=.
in main function the i=.
in main function the i=.
#include <stdio.h>
#include <pthread.h> void* thread_test(void* argv)
{
int i; for(i=;i<;i++)
{
printf("in thread_test the i =%d.\n",i);
sleep();
} pthread_exit((void*));
} int main(void)
{
unsigned int i;
int* thread_val=NULL;
pthread_t pthread; if( pthread_create(&pthread,NULL,thread_test,(void*)&thread_val) )
{
perror("pthread create");
}
if(pthread_detach(pthread))
{
perror("pthread detach");
} for(i=;i<;i++)
{
printf("in main function the i=%d.\n",i);
}
return ;
}
[root@localhost thread]# gcc main.c -lpthread
[root@localhost thread]# ./a.out | grep test
in thread_test the i =.
in thread_test the i =.
in thread_test the i =.
in thread_test the i =.
[root@localhost thread]# cat main.c
#include <stdio.h>
#include <pthread.h> void* thread_test(void* argv)
{
int i; for(i=;i<;i++)
{
printf("in thread_test the i =%d.\n",i);
sleep();
} pthread_exit((void*));
} int main(void)
{
unsigned int i;
int* thread_val=NULL;
pthread_t pthread; //创建一个新的线程
if( pthread_create(&pthread,NULL,thread_test,(void*)&thread_val) )
{
perror("pthread create");
}
if(pthread_detach(pthread))
{
perror("pthread detach");
} for(i=;i<;i++)
{
printf("in main function the i=%d.\n",i);
sleep();
} return ;
}
[root@localhost thread]# ./a.out
in main function the i=.
in thread_test the i =. //可以发现线程交替执行
in main function the i=.
in thread_test the i =.
in main function the i=.
in thread_test the i =.
in main function the i=.
in thread_test the i =.
in main function the i=.
root@localhost thread]# cat main.c
#include <stdio.h>
#include <pthread.h> void* thread_test(void* argv)
{
int i;
pthread_detach(pthread_self()); //通过pthread_self()获取本线程的线程标志
for(i=;i<;i++)
{
printf("in thread_test the i =%d.\n",i);
sleep();
}
pthread_exit((void*));
} int main(void)
{
unsigned int i;
int* thread_val=NULL;
pthread_t pthread;
//创建一个新的线程
if(pthread_create(&pthread,NULL,thread_test,NULL))
{
perror("pthread create");
}
for(i=;i<;i++)
{
printf("in main function the i=%d.\n",i);
sleep();
}
return ;
}
[root@localhost thread]# ./a.out
in main function the i=.
in thread_test the i =.
in main function the i=.
in thread_test the i =.
in main function the i=.
in thread_test the i =.
in main function the i=.
in thread_test the i =.
in main function the i=.
SYNOPSIS
#include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, //待初始化互斥量的指针
const pthread_mutexattr_t *restrict attr //待初始化互斥量要设置的属性
);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //静态初始化互斥量
int pthread_mutex_destroy(pthread_mutex_t *mutex //要销毁初始化状态互斥量的指针
);
SYNOPSIS
#include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex //要获取的互斥锁的指针
);
int pthread_mutex_trylock(pthread_mutex_t *mutex //要获取的互斥锁的指针
);
int pthread_mutex_unlock(pthread_mutex_t *mutex //要释放的互斥锁的指针
);
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> typedef struct {
int fd;
pthread_mutex_t p_mutex;
}p_fd_t; void* thread_test(void* argv)
{
int i;
int j;
int size;
char buf[];
p_fd_t* fd_mutex; pthread_detach(pthread_self()); //将传递进来的参数变为结构题指针
fd_mutex=(p_fd_t *)argv; //加锁
pthread_mutex_lock(&fd_mutex->p_mutex);
for(i=;i<;i++)
{
j=;
size=sprintf(buf,"the pthred id is: %d\n",pthread_self());
while(j<size)
{
write(fd_mutex->fd,&buf[j++],);
usleep();
}
}
//解锁
pthread_mutex_unlock(&fd_mutex->p_mutex); pthread_exit((void*));
} int main(void)
{
unsigned int i;
int j;
int size;
int fd;
char buf[];
pthread_t pthread;
p_fd_t fd_mutex=
{
.p_mutex=PTHREAD_MUTEX_INITIALIZER, //初始化线程互斥锁
}; //打开或者创建一个文件
fd=open("./test",O_RDWR | O_CREAT | O_TRUNC, );
if(- == fd)
{
perror("open ./test");
exit();
} fd_mutex.fd=fd;
//创建一个新的线程
if(pthread_create(&pthread,NULL,thread_test,(void*)&fd_mutex))
{
perror("pthread create");
} //加锁
pthread_mutex_lock(&fd_mutex.p_mutex);
for(i=;i<;i++)
{
j=;
size=sprintf(buf,"the main pthred id is: %d\n",pthread_self());
while(j<size)
{
write(fd_mutex.fd,&buf[j++],);
usleep();
}
}
//解锁
pthread_mutex_unlock(&fd_mutex.p_mutex); sleep(); //加上时间等待 线程结束
//在主线程结束之间销毁线程互斥锁资源
pthread_mutex_destroy(&fd_mutex.p_mutex);
close(fd); return ;
}
[root@localhost thread]# cat test
the main pthred id is: -
the main pthred id is: -
the main pthred id is: -
the main pthred id is: -
the main pthred id is: - //前面的是主线程写入文件的内容,后面是线程写入到内容
the pthred id is: -
the pthred id is: -
the pthred id is: -
the pthred id is: -
the pthred id is: -
[root@localhost thread]#
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> typedef struct {
int fd;
pthread_mutex_t p_mutex;
}p_fd_t; void* thread_test(void* argv)
{
int i;
int j;
int size;
char buf[];
p_fd_t* fd_mutex; pthread_detach(pthread_self()); //将传递进来的参数变为结构题指针
fd_mutex=(p_fd_t *)argv; //加锁
/*pthread_mutex_lock(&fd_mutex->p_mutex);*/
for(i=;i<;i++)
{
j=;
size=sprintf(buf,"the pthred id is: %d\n",pthread_self());
while(j<size)
{
write(fd_mutex->fd,&buf[j++],);
usleep();
}
}
//解锁
/*pthread_mutex_unlock(&fd_mutex->p_mutex);*/ pthread_exit((void*));
} int main(void)
{
unsigned int i;
int j;
int size;
int fd;
char buf[];
pthread_t pthread;
p_fd_t fd_mutex=
{
.p_mutex=PTHREAD_MUTEX_INITIALIZER, //初始化线程互斥锁
}; //打开或者创建一个文件
fd=open("./test",O_RDWR | O_CREAT | O_TRUNC, );
if(- == fd)
{
perror("open ./test");
exit();
} fd_mutex.fd=fd;
//创建一个新的线程
if(pthread_create(&pthread,NULL,thread_test,(void*)&fd_mutex))
{
perror("pthread create");
} //加锁
/*pthread_mutex_lock(&fd_mutex.p_mutex);*/
for(i=;i<;i++)
{
j=;
size=sprintf(buf,"the main pthred id is: %d\n",pthread_self());
while(j<size)
{
write(fd_mutex.fd,&buf[j++],);
usleep();
}
}
//解锁
/*pthread_mutex_unlock(&fd_mutex.p_mutex);*/ sleep(); //加上时间等待 线程结束
//在主线程结束之间销毁线程互斥锁资源
pthread_mutex_destroy(&fd_mutex.p_mutex);
close(fd); return ;
}
[root@localhost thread]# vim pthread.mutex.c
[root@localhost thread]# gcc pthread.mutex.c -lpthread
[root@localhost thread]# ./a.out
[root@localhost thread]# cat test
tthhee m apitnh prtehdr eidd iids :i s-: -
44t
thhee mapitnh rpetdh riedd i di sis:: --
0t8h
e tmhaein pptthhrreedd iidd iiss:: -- tthhee mpatihnr epdt hirded iisd: i-s1: -
4t4
hteh ep tmhariend pitdh riesd: i-d1 2i0s8: - [root@localhost thread]#
【linux草鞋应用编程系列】_4_ 应用程序多线程的更多相关文章
- 【linux草鞋应用编程系列】_5_ Linux网络编程
一.网络通信简介 第一部分内容,暂时没法描述,内容实在太多,待后续专门的系列文章. 二.linux网络通信 在linux中继承了Unix下“一切皆文件”的思想, 在linux中要实现网 ...
- 【linux草鞋应用编程系列】_6_ 重定向和VT100编程
一.文件重定向 我们知道在linux shell 编程的时候,可以使用文件重定向功能,如下所示: [root@localhost pipe]# echo "hello world&q ...
- 【linux草鞋应用编程系列】_3_ 进程间通信
一.进程间通信 linux下面提供了多种进程间通信的方法, 管道.信号.信号量.消息队列.共享内存.套接字等.下面我们分别 介绍管道.信号量.消息队列.共享内存. 信号和套 ...
- 【linux草鞋应用编程系列】_2_ 环境变量和进程控制
一. 环境变量 应用程序在执行的时候,可能需要获取系统的环境变量,从而执行一些相应的操作. 在linux中有两种方法获取环境变量,分述如下. 1.通过main函数的参数获取环境变量 ...
- 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
最近学习linux系统下的应用编程,参考书籍是那本称为神书的<Unix环境高级编程>,个人感觉神书不是写给草鞋看的,而是 写给大神看的,如果没有一定的基础那么看这本书可能会感到有些头重脚轻 ...
- linux高性能服务器编程 (八) --高性能服务器程序框架
第八章 高性能服务器编程框架 这一章主要介绍服务器的三个主要模块: I/O处理单元.逻辑单元.存储单元.另外服务器的模型有:C/S模型和P2P模型.虽然服务器模型比较多,但是其核心框架都一样,只是在于 ...
- Linux高性能服务器编程:高性能服务器程序框架
服务器有三个主要模块: (1)I/O处理单元 (2)逻辑单元 (3)存储单元 1.服务器模型 C/S模型 逻辑:服务器启动后,首先创建一个或多个监听socket,并调用bind函数将其绑定到服务器感兴 ...
- MapReduce 编程 系列七 MapReduce程序日志查看
首先,假设须要打印日志,不须要用log4j这些东西,直接用System.out.println就可以,这些输出到stdout的日志信息能够在jobtracker网站终于找到. 其次,假设在main函数 ...
- 介绍linux设备驱动编程
目前,Linux软件工程师大致可分为两个层次: (1)Linux应用软件工程师(Application Software Engineer): 主要利用C库函数和Linux API进行应用 ...
随机推荐
- 为jQuery添加Webkit的触摸方法支持
前些日子收到邮件,之前兼职的一个项目被转给了其他人,跟进的人来问我相关代码的版权问题. 我就呵呵了. 这段代码是我在做13年一份兼职的时候无聊加上去的,为jQuery添加触摸事件的支持.因为做得有点无 ...
- JavaScript面试时候的坑洼沟洄——表达式与运算符
上篇博客JavaScript面试时候的坑洼沟洄--数据类型总结了一下JavaScript数据类型几转换的相关知识,很多朋友可能和我一样,买了书后对数据类型啊.运算符啊.语句啊都是扫两眼或直接略过的,自 ...
- JavaScript算法(归并排序与快速排序)
归并排序与快速排序这两个算法放在一起,也是因为时间复杂度都是对数级别的. 目前看过的资料,归并排序看<学习JavaScript数据结构与算法>介绍的归并排序吧,快速排序直接看百度百科,讲的 ...
- Intellij IDEA 13.1.3 创建Java Web项目
作者QQ:1095737364 1. Intellij 编辑工具,找到File-->New Project 2. 打开后点击java-->选中WebApplication 和Java ...
- rem与px的转换
rem与px的转换 引用自http://caibaojian.com/rem-and-px.html A-A+ 前端博客•前端开发教程•rem•3702View0 rem是相对于根元素<html ...
- GitHub iOS-Top 100 简介
GitHub排名前100的iOS第三方汇总简介,方便开发者选择适合的第三方框架. 项目名称 项目信息 1. AFNetworking 作者是 NSHipster 的博主, iOS 开发界的大神级人物, ...
- iOS-----dSYM 文件分析工具
来到新公司后,前段时间就一直在忙,前不久 项目 终于成功发布上线了,最近就在给项目做优化,并排除一些线上软件的 bug,因为项目中使用了友盟统计,所以在友盟给出的错误信息统计中能比较方便的找出客户端异 ...
- Java Math 取整的方式
1.Math.floor floor,英文原意:地板. Math.floor 函数是求一个浮点数的地板,就是 向下 求一个最接近它的整数,它的值肯定会小于或等于这个浮点数. 2.Math.ceil c ...
- datagrid界面,链接数据库读取数据
1.学生列表的 HTML部分 <script type="text/javascript"> $(function(){ //创建dataGrid $("#d ...
- 【Win 10开发】协议-上篇:自定义应用协议
就像系统许多内置应用可以通过URI来启动(如ms-settings-bluetooth:可以打开蓝牙设置页),我们自己开发的应用程序,如果需要的话,可以为应用程序自定义一个协议.应用程序协议在安装时会 ...