【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进行应用 ...
随机推荐
- [转]各种移动GPU压缩纹理的使用方法
介绍了各种移动设备所使用的GPU,以及各个GPU所支持的压缩纹理的格式和使用方法.1. 移动GPU大全 目前移动市场的GPU主要有四大厂商系列:1)Imagination Technologies的P ...
- 磁盘分区在windows和linux下的表现形式对比
写在前面的话:磁盘分区在windows下面比较好理解,在linux下会有挂载的概念,理解起来比较难,但是可以通过与windows对比,以一种通俗的方式将他们梳理清楚. ====正文开始==== 我们的 ...
- Tomcat7基于Redis的Session共享实战一
本文主要介绍如何使用redis对tomcat7的session进行托管. 1.安装Redisredis安装比较简单,此处略过. 2.配置两个Tomcat在本机上配置两个Tomcat,分别为tomcat ...
- Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字
不说废话,贴代码: CREATE OR REPLACE FUNCTION to_time(sec IN NUMBER) RETURN VARCHAR2 IS /*把秒转成时分秒格式 auth lzpo ...
- Bower : ENOGIT git is not installed or not in the PATH
解决方法一: 添加git到window的环境变量中.设置path路径为C:\Program Files\Git\bin 解决方法二: $ set PATH=%PATH%;C:\Program File ...
- supportRequestWindowFeature与requestWindowFeature
在Activity中去掉标题栏只需要在onCreate()中在setContentView前使用requestWindowFeature(). 在AppCompatActivity中去掉标题栏只需要在 ...
- eclipse将android项目生成apk并且给apk签名
转载:http://www.cnblogs.com/tianguook/archive/2012/09/27/2705724.html 生成apk最懒惰的方法是:只要你运行过android项目,到工作 ...
- document.getElementById()与 $()区别
document.getElementById()返回的是DOM对象,而$()返回的是jQuery对象 什么是jQuery对象? ---就是通过jQuery包装DOM对象后产生的对象.jQuery对象 ...
- ConcurrentDictionary线程不安全么,你难道没疑惑,你难道弄懂了么?
前言 事情不太多时,会时不时去看项目中同事写的代码可以作个参考或者学习,个人觉得只有这样才能走的更远,抱着一副老子天下第一的态度最终只能是井底之蛙.前两篇写到关于断点传续的文章,还有一篇还未写出,后续 ...
- NuGet程序包安装SQLite后完全抽离出SQLite之入门介绍及注意事项,你真的懂了吗?
前言 近几天的几篇文章讲的内容非前面内容如系列的讲解,这几天文章都是我在项目中遇到的问题以及重新学习的知识,所以和大家分享一下,关于SQLite的文章多如牛毛,但是有些大多已经过时,为什么说过时,之前 ...