Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of
multiple units of execution within a single process
2. 多线程
3. 线程模型
4. 并发、并行、竞争
x++; // x is an integer

这个例子说明了并发的情况,如果是并行,则情况如下:

5. 同步
6. Pthread
#include <pthread.h>
int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
const pthread_t me = pthread_self ();
int pthread_equal (pthread_t t1, pthread_t t2);
#include <pthread.h>
int pthread_cancel (pthread_t thread);
#include <pthread.h>
int pthread_join (pthread_t thread, void **retval);
int ret;
/* join with `thread' and we don't care about its return value */
ret = pthread_join (thread, NULL);
if (ret) {
errno = ret;
perror ("pthread_join");
return -;
}
#include <pthread.h>
int pthread_detach (pthread_t thread);
综合示例:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
void * start_thread (void *message)
{
printf ("%s\n", (const char *) message);
return message;
}
int main (void)
{
pthread_t thing1, thing2;
const char *message1 = "Thing 1";
const char *message2 = "Thing 2";
/* Create two threads, each with a different message. */
pthread_create (&thing1, NULL, start_thread, (void *) message1);
pthread_create (&thing2, NULL, start_thread, (void *) message2);
/*
* Wait for the threads to exit. If we didn't join here,
* we'd risk terminating this main thread before the
* other two threads finished.
*/
pthread_join (thing1, NULL);
pthread_join (thing2, NULL);
return ;
}
gcc -Wall -O2 -pthread example.c -o example
/* define and initialize a mutex named `mutex' */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
(6) 加锁、解锁
int pthread_mutex_lock (pthread_mutex_t *mutex);
int pthread_mutex_unlock (pthread_mutex_t *mutex);
class ScopedMutex {
public:
ScopedMutex (pthread_mutex_t& mutex)
:mutex_ (mutex)
{
pthread_mutex_lock (&mutex_);
}
~ScopedMutex ()
{
pthread_mutex_unlock (&mutex_);
}
private:
pthread_mutex_t& mutex_;
};
Linux System Programming 学习笔记(七) 线程的更多相关文章
- Linux System Programming 学习笔记(十一) 时间
1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...
- Linux System Programming 学习笔记(六) 进程调度
1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...
- Linux System Programming 学习笔记(四) 高级I/O
1. Scatter/Gather I/O a single system call to read or write data between single data stream and mu ...
- Linux System Programming 学习笔记(三) 标准缓冲I/O
1. partial block operations are inefficient. The operating system has to “fix up” your I/O by ensuri ...
- Linux System Programming 学习笔记(二) 文件I/O
1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...
- Linux System Programming 学习笔记(一) 介绍
1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...
- Linux System Programming 学习笔记(十) 信号
1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0) 内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...
- Linux System Programming 学习笔记(九) 内存管理
1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...
- Linux System Programming 学习笔记(八) 文件和目录管理
1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...
随机推荐
- linux - centos7 开放防火墙端口的新方式
CentOS 升级到7之后,发现无法使用iptables控制Linuxs的端口, google之后发现Centos 7使用firewalld代替了原来的iptables. 下面记录如何使用firewa ...
- 数据类型-------JavaScript
之前只是简单的学过JavaScript和JQuery,虽然一般的要求都能完成,但并没有深入,这次是看了一个网站,很详细的教学,想重新认识一下JavaScript和JQuery. 本文摘要:http:/ ...
- 利用SignalR实现实时聊天
2018/10/10:博主第一次写原创博文而且还是关于C#的(博主是从前端转过来的),菜鸟一枚,如果有什么写的不对,理解错误,还望各位轻喷.,从SignalR开始! 首先先介绍一下关于SignalR的 ...
- Pig Latin-freecodecamp算法题目
Pig Latin 1.要求 Pig Latin把一个英文单词的第一个辅音或辅音丛(consonant cluster)移到词尾,然后加上后缀 "ay". 如果单词以元音开始,你只 ...
- 【bitset 技巧 分块】bzoj5087: polycomp
神仙zq发现了${n^2\sqrt n}\over 32$做法 Description 你有三个系数为0,1的多项式f(x),g(x),h(x) 求f(g(x)) mod h(x) 为方便起见,将答案 ...
- k8s的pod生命周期
pod的生命周期: 1.init container 2.main contianer (1) post start hook :容器启动后做什么操作(可以命令查看kubectl explain po ...
- linux下的一些命令分析与shell的一些命令
对> 与 >>的理解 echo "aaa" > aaa.txt 这个是在aaa.txt中写入aaa 可以用cat aaa.txt查看 echo &qu ...
- Django之模型---ORM 多表操作
多表操作 创建表模型 from django.db import models # Create your models here. class Author(models.Model): nid = ...
- UVA1589——xiangqi
开始碰到这个题时觉得太麻烦了直接跳过没做,现在放假了再次看这个题发现没有想象中那么麻烦,主要是题目理解要透彻,基本思路就是用结构体数组存下红方棋子,让黑将军每次移动一下,看移动后是否有一个红方棋子可以 ...
- Elasticsearch安装---安装jdk
1.在Linux 上检查Java版本是否满足要求: java -version 如果运行上面命令时报错:-bash: java: command not found,首先检查是否有jdk ,要是有安装 ...