APUE学习之多线程编程(一):线程的创建和销毁
#include <pthread.h>
pthread_t pthread_self(void);
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_arrt_t *restrict addr, void *(*start_rtn)(void *), void *restrict arg);
#include "apue.h"
#include <pthread.h> pthread_t ntid; void printids(const char *s)
{
pid_t pid;
pthread_t tid; pid = getpid();
tid = pthread_self();
printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid, (unsigned long)tid, (unsigned long)tid);
} void *thr_fn(void *arg)
{
printids("new thread: ");
return ((void *));
} int main(void)
{
int err; err = pthread_create(&ntid, NULL, thr_fn, NULL);
if (err != )
{
err_exit(err, "can't create thread");
} printids("main thread: ");
sleep();
exit();
}
./a.out
main thread: pid tid (0xb75e36c0)
new thread: pid tid (0xb75e2b40)
#include <pthread.h>
void pthread_exit(void *rval_ptr);
ravl_ptr是无类型指针,进程中的其他线程可以通过pthread_join函数获得这个指针。
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
调用线程将一直阻塞,直至指定的线程退出,rval_ptr就包含返回码,如果线程被取消,rval_ptr指定的内存单元就设置为PTHREAD_CANCELED.可以通过调用pthread_join自动把线程置于分离状态,如果线程已处于分离状态,pthread_join就会调用失败。
#include "apue.h"
#include <pthread.h> void *thr_fn1(void *arg)
{
printf("thread 1 returning\n");
return (void *);
} void *thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *));
} int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != )
{
err_exit(err, "can't create thread1");
} err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err != )
{
err_exit(err, "can't create thread2");
} err = pthread_join(tid1, &tret); if (err != )
{
err_exit(err, "can't join thread1");
} printf("thread1 exit code:%ld\n", (long)tret); err = pthread_join(tid2, &tret); if (err != )
{
err_exit(err, "can't join thread2");
} printf("thread2 exit code:%ld\n", (long)tret); return ;
}
./a.out
thread exiting
thread returning
thread1 exit code:
thread2 exit code:
#include <pthread.h>
int pthread_cancel(pthread_t tid);
听着有点霸道,不过也只是请求而已,线程可以选择忽略这个请求。
#include <pthread.h>
void pthread_cleanup_push(void(*rtn)(void *), void *arg);
void pthread_cleanup_pop(int execute);
当线程执行以下动作时,清理函数rtn由pthread_cleanup_push函数调度
#include "apue.h"
#include <pthread.h> void cleanup(void *arg)
{
printf("cleanup: %s\n", (char *)arg);
} void *thr_fn1(void *arg)
{
printf("thread 1 start\n");
pthread_cleanup_push(cleanup, "thread 1 first handler");
pthread_cleanup_push(cleanup, "thread 1 second handler");
printf("thread 1 push complete\n"); if (arg)
{
return (void *);
} pthread_cleanup_pop();
pthread_cleanup_pop(); return (void *);
} void *thr_fn2(void *arg)
{
printf("thread 2 start\n");
pthread_cleanup_push(cleanup, "thread 2 first handler");
pthread_cleanup_push(cleanup, "thread 2 second handler");
printf("thread 2 push complete\n"); if (arg)
{
pthread_exit((void *));
} pthread_cleanup_pop();
pthread_cleanup_pop(); return (void *);
} int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret; err = pthread_create(&tid1, NULL, thr_fn1, (void *));
if (err != )
{
err_exit(err, "can't create thread 1");
} err = pthread_create(&tid2, NULL, thr_fn2, (void *));
if (err != )
{
err_exit(err, "can't create thread 2");
} err = pthread_join(tid1, &tret);
if (err != )
{
err_exit(err, "can't join with thread 1");
}
printf("thread 1 exit code %ld\n", (long)tret); err = pthread_join(tid2, &tret);
if (err != )
{
err_exit(err, "can't join with thread 2");
}
printf("thread 2 exit code %ld\n", (long)tret); return ;
}
./a.out
thread start
thread push complete
cleanup: thread second handler
cleanup: thread first handler
thread start
thread push complete
thread exit code
thread exit code
#include <pthread.h>
int pthread_detach(pthread_t tid);
APUE学习之多线程编程(一):线程的创建和销毁的更多相关文章
- APUE学习之多线程编程(三):线程属性、同步属性
一.线程属性 可以使用pthread_attr_t结构修改线程默认属性,并这些属性和创建的线程练习起来,可以使用pthread_att_init函数初始化pthread_attr_t结构,调 ...
- APUE学习之多线程编程(二):线程同步
为了保证临界资源的安全性和可靠性,线程不得不使用锁,同一时间只允许一个或几个线程访问变量.常用的锁有互斥量,读写锁,条件变量 一.互斥量 互斥量是用pthrea ...
- .NET面试题解析(07)-多线程编程与线程同步
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实是很多的,比如多线程编程.线程上下文.异步编程.线程同步构造.GUI的跨线程访问等等, ...
- .NET面试题解析(07)-多线程编程与线程同步 (转)
http://www.cnblogs.com/anding/p/5301754.html 系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实 ...
- Linux 系统编程 学习:09-线程:线程的创建、回收与取消
Linux 系统编程 学习:09-线程:线程的创建.回收与取消 背景 我们在此之前完成了 有关进程的学习.从这一讲开始我们学习线程. 完全的开发可以参考:<多线程编程指南> 在Linux ...
- vc 基于对话框多线程编程实例——线程之间的通信
vc基于对话框多线程编程实例——线程之间的通信 实例:
- Python中的多线程编程,线程安全与锁(二)
在我的上篇博文Python中的多线程编程,线程安全与锁(一)中,我们熟悉了多线程编程与线程安全相关重要概念, Threading.Lock实现互斥锁的简单示例,两种死锁(迭代死锁和互相等待死锁)情况及 ...
- Python中的多线程编程,线程安全与锁(一)
1. 多线程编程与线程安全相关重要概念 在我的上篇博文 聊聊Python中的GIL 中,我们熟悉了几个特别重要的概念:GIL,线程,进程, 线程安全,原子操作. 以下是简单回顾,详细介绍请直接看聊聊P ...
- C#多线程编程实例 线程与窗体交互
C#多线程编程实例 线程与窗体交互 代码: public partial class Form1 : Form { //声明线程数组 Thread[] workThreads = ]; public ...
随机推荐
- PHP安全之Web攻击
一.SQL注入攻击(SQL Injection) 攻击者把SQL命令插入到Web表单的输入域或页面请求的字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响)动态 ...
- 查看Job执行的历史记录
SQL Server将Job的信息存放在msdb中,Schema是dbo,表名以“sysjob”开头. 一,基础表 1, 查看Job和Step,Step_ID 是从1 开始的. select j.jo ...
- LLBL Gen Pro 5.0 企业应用开发入门
Solutions Design 公司于2016年5月发布了LLBL Gen Pro 5.0,这个新版本的发布出乎于我的意料.我的猜想是从4.2升级到4.5,再升级5.x版本,主版本号的变更会给原有客 ...
- Git(远程仓库:git@oschina)-V2.0
1.注册git@osc(也就是“码云”) 这里会提示注册密码==push密码,反正一定要记住的东西. 2.安装git 这里要设置个人信息 git config --list //查看git信息 g ...
- How to debug .NET Core RC2 app with Visual Studio Code on Windows?
Simone Chiaretta (http://codeclimber.net.nz/archive/2016/05/20/How-to-debug-NET-Core-RC2-app-with-Vi ...
- php中会话保持 session 与cooker
会话保持 1.session Session:在计算机中,尤其是在网络应用中,称为"会话控制".Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 ...
- Android面试一天一题(1Day)
写在前面 该博客思路源于在简书看到goeasyway博主写的Android面试一天一题系列,无copy之意,仅为让自己总结知识点,成长一点点.先感谢各位大神的无私分享~! 关于题目,大部分则出自And ...
- 你真的会玩SQL吗?玩爆你的数据报表之存储过程编写(上)
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- jQuery2.x源码解析(构建篇)
jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 笔者阅读了园友艾伦 Aaron的系列博客< ...
- ASP.NET Core 中文文档 第三章 原理(14)服务器
原文:Servers 作者:Steve Smith 翻译:谢炀(Kiler) 校对:许登洋(Seay).姚阿勇(Dr.Yao) ASP.NET Core 已完全从承载应用程序的 Web 服务器环境中分 ...