Linux 线程编程1.0
在编译多线程程序的时候,需要连接libpthread文件:
gcc pthread.c -o pthread -lpthread;
所有线程一律平等,没有父子关系,线程属于进程。
创建线程用 pthread_create()函数,其函数原型是:
#include<pthread.h>
int pthread_create(pthread_t *restrict thread,const pthread_attr_t *restric attr,void* (*start_routine)(void *),void *restrict arg);
第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,大多数情况下,
我们不需要设置线程的属性,那么空指针 NULL 就可以了,第三个参数是线程运行函数的起
始地址,最后一个参数是运行函数的参数。
当创建线程成功时,函数返回 0,若不为 0 则说明创建线程失败。
创建代码如下:
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
void *thread(void*agrv)
{
int i;
for(i=;i<;i++)
{
printf("run pthread..\n");//sleep(1); }
} int main()
{ int i;
int ret;
pthread_t pthid;//创建线程标识符
ret=pthread_create(&pthid,NULL,thread,NULL);
if(ret!=)
printf("create pthread error!\n"); for(i=;i<;i++)
{
printf("run main..\n");//sleep(1); }
}
注释掉sleep(1)后输出结果为:
run main..
run main..
run main..
run main..
run main..
这是因为主线程结束以后进程就直接结束了。我们可以加一个sleep函数,等待子线程的完成。
加入sleep(1)后结果为:
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
可以看出两个线程的输出是交替的,当一个线程进入睡眠时,另外一个线程就会被调度执行。
接下来修改一下程序,让子进程里面的程序循环10次:此时,由于在main函数中,主线程输出5次进程就会结束,所以子线程里面剩余的输出将不会进行,输出结果会和上面的一样。我们可以添加一个等待函数,注意的是,一个线程不能被多个线程等待!!
pthread_join():这个函数是主线程用于等待子线程结束的,返回值为int型;其函数原型是:
#include<pthread.h>
int pthread_join(pthread_t thread,void **value_ptr);
第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。
代码为:
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
void *thread(void*agrv)
{
int i;
for(i=;i<;i++)
{
printf("run pthread..\n");sleep(); }
} int main()
{ int i;
int ret;
pthread_t pthid;//创建线程标识符
ret=pthread_create(&pthid,NULL,thread,NULL);
// pthread_join( pthid,NULL); // 等待函数放在这会先把子线程打印完毕再打印主线程里面的内容
if(ret!=)
printf("create pthread error!\n"); for(i=;i<;i++)
{
printf("run main..\n");sleep(); }
pthread_join(pthid,NULL);//一般放在末尾
}
输出结果为:
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
一个线程的结束有两种途径,一种是和示例 3-10 一样,函数结束了,调用它的线程也
就结束了:另一种方式是通过函数 pthread_exit()来实现。它的函数原型为:
#include<pthread.h>
void pthread_exit(void *value_ptr)
函数的参数是函数的返回代码,只要 pthread_join 中的第二个参数 value_ptr 不是NULL,这个值将被传递给主线程。
代码如下:
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
void *thread(void*agrv)
{
int i;
for(i=;i<;i++)
{
printf("run pthread..\n");sleep(); }
} int main()
{ int i;
int ret;
pthread_t pthid;//创建线程标识符
ret=pthread_create(&pthid,NULL,thread,NULL); if(ret!=)
printf("create pthread error!\n"); pthread_exit(NULL);//主线程结束,但是程序并没有结束,会打印子线程的内容 for(i=;i<;i++)
{
printf("run main..\n");sleep(); }
}
输出结果为:
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
Linux 线程编程1.0的更多相关文章
- Linux 线程编程2.0——线程同步-互斥锁
当我们需要控制对共享资源的存取的时候,可以用一种简单的加锁的方法来控制.我们可以创建一个读/写程序,它们共用一个共享缓冲区,使用互斥锁来控制对缓冲区的存取. 函数 pthread_mutex_init ...
- Linux 线程编程3.0
#include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h& ...
- Linux线程编程之信号处理
前言 Linux多线程环境中的信号处理不同于进程的信号处理.一方面线程间信号处理函数的共享性使得信号处理更为复杂,另一方面普通异步信号又可转换为同步方式来简化处理. 本文首先介绍信号处理在进程中和线程 ...
- linux 线程编程详解
1.线程的概念: 线程和进程有一定的相似性,通常称为轻量级的进程 同一进程中的多条线程将共享该进程中的全部系统资源,如虚拟地址空间,文件描述符和信号处理等等.但同一进程中的多个线程都有自身控制流 (它 ...
- Linux线程编程之生产者消费者问题
前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注意的事项.文中涉及的代码运行环境如下: 本文假定读者已具备线程同步的基础知识. 一 顺序表循环队列 1.1 ...
- Linux线程编程之生产者消费者问题【转】
转自:http://www.cnblogs.com/clover-toeic/p/4029269.html 前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注 ...
- Linux多任务编程——线程
线程基础 △ 由于进程的地址空间是私有的,因此在进行上下文切换时,系统开销比较大 △ 在同一个进程中创建的线程共享该进程的地址空间 △ 通常线程值得是共享相同地址空间的多个任务 △ 每个线程的私有这些 ...
- linux高级编程基础系列:线程间通信
linux高级编程基础系列:线程间通信 转载:原文地址http://blog.163.com/jimking_2010/blog/static/1716015352013102510748824/ 线 ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...
随机推荐
- 让UITableView的section header view不悬停的方法
当 UITableView 的 style 属性设置为 Plain 时,这个tableview的section header在滚动时会默认悬停在界面顶端.取消这一特性的方法有两种: 将 style 设 ...
- 编程语言分类,安装python解释器,变量
1.编程语言分类 机器语言:直接使用二进制指令去编写程序,直接操作硬件 优点:执行效率高 缺点:开发效率低 汇编语言:用英文标签取代二进制指令去编写程序,直接进操作硬件 优点:开发效率高于机器语言 缺 ...
- spring 大会的启示
1.事件驱动的微服务编程 2.无服务架构的编程模型 3.微服务缓存
- leetcode15
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); Li ...
- React.js 学习笔记
React.js React.js 是时下最流行的前端 JavaScript 框架之一. 创建工程 # 安装 CLI $ npm install -g create-react-app # 创建新的应 ...
- Android Studio--按钮跳转新页
MainActivity.xml: <Button android:id="@+id/btnGo" android:layout_width="wrap_conte ...
- 前端 跨Area后Cookie无法访问
创建两个区域,一个是User,一个是Manage. User区域有两个页面,index1,和index2 User区域: index1:负责写入cookie index2:负责读取cookie Man ...
- Lua中面向对象
一.Lua中类的简单实现: (1)版本——摘自 Cocos2.0中的: --Create an class. function class(classname, super) local superT ...
- 亿级 ELK 日志平台构建部署实践
本篇主要讲工作中的真实经历,我们怎么打造亿级日志平台,同时手把手教大家建立起这样一套亿级 ELK 系统.日志平台具体发展历程可以参考上篇 「从 ELK 到 EFK 演进」 废话不多说,老司机们座好了, ...
- echarts仪表盘
echarts链接:https://gallery.echartsjs.com/editor.html?c=xkasbcOqh0 代码: var axislineColor = new echarts ...