线程与进程关键字对比

创建新流 fork/pthread_create

退出控制流 exit/pthread_exit

获取退出状态 waitpid/pthread_join

在退出时的清理工作 atexit/pthread_cleanup_push

非正常退出 abort/pthread_cancel

创建线程

int pthread_create(线程ID返回值, 线程属性, 任务地址, 任务附加参数)

获取线程ID: pthread_t pthread_self(void)

判断是否同一线程: int pthread_equal(pthread_t tid1, pthread_t tid2)

线程终止

线程退出方式: pthread_exit; 线程正常执行完毕返回; 被同一进程中的其它线程取消

错误退出方式: 调用exit, _exit, _Exit都会使整个进程退出

获取返回值: pthread_join

线程清理

pthread_cleanup_push: 添加一个清理函数, 如果意外退出可以确保资源释放

pthread_cleanup_pop(0): 取消清理函数, 比如主动清理完成时便可以调用pop来取消push的操作

以下三种情况会触发push操作:

  • 调用pthread_exit退出(调用return不会触发)
  • 响应pthread_cancel请求
  • pthread_cleanup_pop(int), int参数为非零时

线程同步

1.互斥量

2.读写锁

3.条件变量

例子

1.创建和退出线程

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> void *
thr_fn1(void *arg){
printf("thread 1 returning \n");
return((void *)1);
} void *
thr_fn2(void *arg){
printf("thread 2 exiting\n");
pthread_exit((void *)2);
} int main(){
int err;
pthread_t tid1,tid2;
void *tret; err=pthread_create(&tid1,NULL,thr_fn1,NULL);
if(err != 0){
perror("pthread_create error");
return -1;
}
err=pthread_create(&tid2,NULL,thr_fn2,NULL);
if(err != 0){
perror("pthread_create error");
return -1;
} err=pthread_join(tid1,&tret);
if(err != 0){
perror("pthread_join error");
return -1;
}
printf("thread 1 exit code %d\n",(int)tret);
err=pthread_join(tid2,&tret);
if(err != 0){
perror("pthread_join error");
return -1;
}
printf("thread 2 exit code %d\n",(int)tret);
return 0;
}

2.清理函数

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> void cleanup(void *arg){
printf("in cleanup : %s\n",arg);
} void *
thr_fn1(void *arg){
puts("thread start");
pthread_cleanup_push(cleanup,"the first time");
pthread_cleanup_push(cleanup,"the second time");
if(1==(int)arg)
return((void*)1);
else if(2==(int)arg)
pthread_exit((void*)2);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return ((void *)0);
} int main(){
int err;
pthread_t tid1;
void *tret; //pthread_create第四个参数,
//0时会执行pop取消清理函数, 1时以return方式退出不触发清理函数,2时以pthread_exit方式退出触发清理函数
err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
if(err != 0){
perror("pthread_create error");
return -1;
} err=pthread_join(tid1,&tret);
if(err != 0){
perror("pthread_join error");
return -1;
}
printf("thread 1 exit code %d\n",(int)tret);
return 0;
}

3.互斥量

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h> struct people{
char name[10];
int age;
int count;
pthread_mutex_t lock;
}; struct people * init(){
struct people *man;
if((man=malloc(sizeof(struct people))) != NULL){
man->count=1;
if(pthread_mutex_init(&man->lock,NULL) != 0){
free(man);
return NULL;
}
strcpy(man->name,"wahaha");
man->age=20;
}
return man;
} void add_count(struct people *man){
pthread_mutex_lock(&man->lock);
puts("----in add_count----");
printf("now count=%d\n",++man->count);
puts("----out add_count----");
pthread_mutex_unlock(&man->lock);
} void del_count(struct people *man){
pthread_mutex_lock(&man->lock);
if(--man->count == 0){
pthread_mutex_unlock(&man->lock);
pthread_mutex_destroy(&man->lock);
puts("last count to del");
free(man);
}else{
pthread_mutex_unlock(&man->lock);
}
} void * thr_fn(void *arg){
add_count(arg);
sleep(1);
del_count(arg);
} int main(){
struct people *man;
man=init();
if(man == NULL){
perror("man init error");
return -1;
} int err;
pthread_t tid1,tid2; err=pthread_create(&tid1,NULL,thr_fn,man);
if(err != 0){
perror("pthread_create error");
return -1;
}
err=pthread_create(&tid2,NULL,thr_fn,man);
if(err != 0){
perror("pthread_create error");
return -1;
} err=pthread_join(tid1,NULL);
if(err != 0){
perror("pthread_join error");
return -1;
}
err=pthread_join(tid2,NULL);
if(err != 0){
perror("pthread_join error");
return -1;
} del_count(man);
return 0;
}

linux 线程笔记的更多相关文章

  1. linux线程笔记1之创建线程

    1 线程与进程的对比 这里有一个笔记详细的阐述 http://blog.csdn.net/laviolette/article/details/51506953 2 创建线程函数 int pthrea ...

  2. Dubbo入门到精通学习笔记(十一):Dubbo服务启动依赖检查、Dubbo负载均衡策略、Dubbo线程模型(结合Linux线程数限制配置的实战分享)

    文章目录 Dubbo服务启动依赖检查 Dubbo负载均衡策略 Dubbo线程模型(结合Linux线程数限制配置的实战分享) 实战经验分享( ** 属用性能调优**): Dubbo服务启动依赖检查 Du ...

  3. Linux线程互斥学习笔记--详细分析

    一.互斥锁 为啥要有互斥? 多个进程/线程执行的先后顺序不确定,何时切出CPU也不确定. 多个进程/线程访问变量的动作往往不是原子的. 1. 操作步骤 (1)创建锁 // 创建互斥锁mutex pth ...

  4. linux线程的实现

    首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个分身可以处理一件特定事情.这在处理异步事件如异步IO时特别有用.内核线程的使用是廉价的,唯一使用 ...

  5. 跟着鸟哥学Linux系列笔记3-第11章BASH学习

    跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 跟着鸟哥学Linux系列笔记1 跟着鸟哥学Linux系列笔记2-第10章VIM学习 认识与学习bash 1. ...

  6. linux线程的实现【转】

    转自:http://www.cnblogs.com/zhaoyl/p/3620204.html 首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个 ...

  7. Linux 读书笔记 二

    一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 若不小心登出后,直接刷新页面即可 2. 环境使用 完成实验后可以点击桌面上方的“实验截图”保存并分 ...

  8. 【转载】linux内核笔记之进程地址空间

    原文:linux内核笔记之进程地址空间 进程的地址空间由允许进程使用的全部线性地址组成,在32位系统中为0~3GB,每个进程看到的线性地址集合是不同的. 内核通过线性区的资源(数据结构)来表示线性地址 ...

  9. # linux读书笔记(3章)

    linux读书笔记(3章) 标签(空格分隔): 20135328陈都 第三章 进程管理 3.1 进程 进程就是处于执行期的程序(目标码存放在某种存储介质上).但进程并不仅仅局限于一段可执行程序代码( ...

随机推荐

  1. iOS 多线程讲解2

    1.GCD其他方法 1.GCD应用 单例模式 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@&qu ...

  2. Unix系统安装MySQL-python出现UnicodeDecodeError错误解决方法

    今天装MySQL-python时候出现了这个错误: error: command ---------------------------------------- Cleaning up... Com ...

  3. jquery plugins —— datatables ajax post更新数据

    通过下面语句,可以定义datatables插件通过ajax post方法从服务器段获取JSON格式的数据. 错误写法(这样写再执行ajax.reload()方法时,ID参数还是初始时,不会更新): v ...

  4. Java中的堆和栈的区别

    当一个人开始学习Java或者其他编程语言的时候,会接触到堆和栈,由于一开始没有明确清晰的说明解释,很多人会产生很多疑问,什么是堆,什么是栈,堆和栈有什么区别?更糟糕的是,Java中存在栈这样一个后进先 ...

  5. 学习Slim Framework for PHP v3 (四)--get()是怎么加进去的?

    看看官网加粗的一句话: At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate ...

  6. 基于asp.net MVC 的服务器和客户端的交互(三)之客户端请求响应

    一.分析 WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: GET 用于获取 URI 资源的进行展示,GET ...

  7. Chrome浏览器跨域问题

    最近在学习Ionic,调试http请求的时候遇到跨域问题 解决办法:使用chrome浏览器安装这个插件:Allow-Control-Allow-Origin: *

  8. Android之画廊点击内容显示

    package com.example.Gallery; import com.example.Gallery.R; import android.os.Bundle; import android. ...

  9. 最新32位和64位xp,win7,win8,win10系统大全(电脑装机版)

    一.系统主要特点 1.安装维护方便快速 - 全自动无人值守安装,采用万能GHOST技术,安装系统过程只需3-5分钟,适 合新旧各种机型. - 集成常见硬件驱动,智能识别+预解压技术,绝大多数硬件可以快 ...

  10. VC中调用COM组件的方法(转载)

    原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...