【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进行应用 ...
随机推荐
- C#设计模式之外观
IronMan之外观 接着上篇观察者内容的“剧情”,没看过的朋友也没关系,篇幅之间有衔接的关系但是影响不大. 需求: 为"兵工厂"提供各种支持,生产了各式各样的"Iron ...
- 分享一个css3写的气泡对话框,适合于即时通讯和留言本的动态内容
效果预览: css code .message_content{width:100%;margin-top:10px;clear:both;float:left;} .face{float:left; ...
- CSS效果集锦(持续更新中)
高亮光弧效果 使用CSS3实现的一个高亮光弧效果,当鼠标hover到某一个元素上时,一道光弧从左向右闪过,效果如下: 代码如下: <!DOCTYPE html> <html lang ...
- Socket programing(make a chat software) summary 1:How to accsess LAN from WAN
First we should know some basic conceptions about network: 1.Every PC is supposed to have its own IP ...
- sublime text 下的Markdown写作
sublime text 2(3)下的Markdown写作 什么是 Markdown wiki Markdown 是一种方便记忆.书写的纯文本标记语言,用户可以使用这些标记符号以最小的输入代价生成极富 ...
- C#设计模式系列:外观模式(Facade)
外观模式主要解决的问题是:当我们有多个类要处理时,往往要一个类一个类地区调用,没有复用性和扩展性.外观模式通过定义一个界面,把处理子类的过程封装成操作,主要就把用户从复杂的调用过程中解放出来. 1.外 ...
- python调取C/C++的dll生成方法
本文针对Windows平台下,python调取C/C++的dll文件. 1.如果使用C语言,代码如下,文件名为test.c. __declspec(dllexport) int sum(int a,i ...
- v-if VS v-show
在vue实现轮播图效果 中分别用到 v-if和 v-show 下面讲讲我理解的他们的区别: v-if: 根据表达式的值的真假条件渲染元素.在切换时元素及它的数据绑定 / 组件被销毁并重建.如果元素是 ...
- HTTP的长连接和短连接——Node上的测试
本文主要从实践角度介绍长.短连接在TCP层面的表现,借助Node.JS搭建后台服务,使用WinHTTP.Ajax做客户端请求测试,最后简单涉及WebSocket. 关键字:长连接.短连 ...
- 【原创】开源Math.NET基础数学类库使用(10)C#进行基本数据统计
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...