linux线程及互斥锁
进程是资源管理的最小单元,线程是程序执行的最小单元。在操作系统的设计上,从进程演化出线程,最主要的目的就是更好的支持SMP以及减小(进程/线程)上下文切换开销。
就像进程有一个PID一样,每个线程也有自己的线程ID,但线程ID只在它所属的环境中有效;
创建一个新的线程可以通过调用pthread_create()函数来创建;
函数原型为:
#include <pthread.h>
int pthread_create(pthread_t *thread_addr_t, *addr, void *(*start_rtn) (void), void *restrict arg;
第一个参数是一个指针,它指向一个pthread_t类型的结构,再创建一个线程时,这个指针指向的变量里会写入
新线程的ID,第二个参数对线程的属性进行设置;一般默认为NULL;
最后两个参数分别是线程将要启动执行的函数以及将要传递给这个函数的参数。
下面的代码是创建一个新的线程:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h> /*新线程要执行的函数*/
void *hello(void *arg)
{
printf("hello world\n");
} int main()
{
pthread_t pid = ;
int ret = ; /*获得一个新的线程*/
ret = pthread_create(&pid, NULL, hello, NULL);
if(ret < ) {
perror("pthread_create");
exit(EXIT_FAILURE);
} /* 检查线程退出状态*/
pthread_join(pid, NULL);
exit(EXIT_SUCCESS);
}
二:互斥锁
下面代码是主线程和工作线程共享用户的输入缓冲,主线程创建了工作线程用于统计用户输入的字符数,它们之间通过互斥锁保证对输入缓冲的访问不发生冲突;
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#define WORK_SIZE 1024
pthread_mutex_t work_mutex;
char work[]; /* 工作区*/
int time_to_exit = ; /*退出标志*/ void *thread_function(void *arg); int main()
{
int ret = ;
pthread_t tid = ;
void *thread_result; /* 对互斥锁进行初始化*/
ret = pthread_mutex_init(&work_mutex, NULL);
if(ret != ) {
perror("mutex init failure");
return ;
} ret = pthread_create(&tid, NULL, thread_function, NULL);
if(ret != ) {
perror("pthread_create");
return ;
} /*给工作区加上锁,把文本读到里面
然后给它解锁允许被其他线程访问*/
pthread_mutex_lock(&work_mutex);
printf("Inpu some txt, Enter 'end' to finish\n");
while(!time_to_exit) {
fgets(work, , stdin);
pthread_mutex_unlock(&work_mutex); while() {
pthread_mutex_lock(&work_mutex);
if(work[] != '\0') {
pthread_mutex_unlock(&work_mutex);
sleep(); }
else
break;
}
}
pthread_mutex_unlock(&work_mutex);
ret = pthread_join(tid, &thread_result);
if(ret != ) {
perror("pthread_join");
return ;
} pthread_mutex_destroy(&work_mutex);
} void *thread_function(void *arg)
{
sleep();
/*新线程试图对互斥量进行加锁*/
pthread_mutex_lock(&work_mutex);
while(strncmp("end", work, ) != ) {
printf("you input %d characters\n", strlen(work) - );
/*把第一个字符设置为空字符已经完成了字符统计工作*/
work[] = '\0';
pthread_mutex_unlock(&work_mutex);
sleep(); /*周期性的尝试给互斥量加锁,如果加锁成功,
就检查主线程是否有新字符需要统计,如果没有,
解开互斥量继续等待*/
pthread_mutex_lock(&work_mutex);
while(work[] == '\0') {
pthread_mutex_unlock(&work_mutex);
sleep();
pthread_mutex_lock(&work_mutex);
}
}
/*设置退出标志*/
time_to_exit = ;
work[] = '\0';
pthread_mutex_unlock(&work_mutex);
pthread_exit();
}
linux线程及互斥锁的更多相关文章
- JoinableQueue队列,线程,线程于进程的关系,使用线程,线程的特点,守护线程,线程的互斥锁,死锁问题,递归锁,信号量
1.JoinableQueue队列 JoinableQueue([maxsize]):这就像是一个Queue对象,但是队列允许项目的使用者通知生成者项目已经被成功处理.通知进程是使用共享的信号和条件变 ...
- Linux 线程编程2.0——线程同步-互斥锁
当我们需要控制对共享资源的存取的时候,可以用一种简单的加锁的方法来控制.我们可以创建一个读/写程序,它们共用一个共享缓冲区,使用互斥锁来控制对缓冲区的存取. 函数 pthread_mutex_init ...
- 并发编程 - 线程 - 1.互斥锁/2.GIL解释器锁/3.死锁与递归锁/4.信号量/5.Event事件/6.定时器
1.互斥锁: 原理:将并行变成串行 精髓:局部串行,只针对共享数据修改 保护不同的数据就应该用不用的锁 from threading import Thread, Lock import time n ...
- Linux再谈互斥锁与条件变量
原文地址:http://blog.chinaunix.net/uid-27164517-id-3282242.html pthread_cond_wait总和一个互斥锁结合使用.在调用pthread_ ...
- linux 2.6 互斥锁的实现-源码分析
http://blog.csdn.net/tq02h2a/article/details/4317211 看了看linux 2.6 kernel的源码,下面结合代码来分析一下在X86体系结构下,互斥锁 ...
- day34 python学习 守护进程,线程,互斥锁,信号量,生产者消费者模型,
六 守护线程 无论是进程还是线程,都遵循:守护xxx会等待主xxx运行完毕后被销毁 需要强调的是:运行完毕并非终止运行 #1.对主进程来说,运行完毕指的是主进程代码运行完毕 #2.对主线程来说,运行完 ...
- Python中线程与互斥锁
了解之前我们先了解一下什么是多任务? 概念: 几个不同的事件在同时运行就是多任务, 这样的话, 我们有牵扯到了真的多任务, 假的多任务; 并行: 真的多任务, 通过电脑的核数来确定 并发: 假的多任务 ...
- Python进阶(3)_进程与线程中的lock(线程中互斥锁、递归锁、信号量、Event对象、队列queue)
1.同步锁 (Lock) 当全局资源(counter)被抢占的情况,问题产生的原因就是没有控制多个线程对同一资源的访问,对数据造成破坏,使得线程运行的结果不可预期.这种现象称为“线程不安全”.在开发过 ...
- Python 开启线程的2中方式,线程VS进程(守护线程、互斥锁)
知识点一: 进程:资源单位 线程:才是CPU的执行单位 进程的运行: 开一个进程就意味着开一个内存空间,存数据用,产生的数据往里面丢 线程的运行: 代码的运行过程就相当于运行了一个线程 辅助理解:一座 ...
随机推荐
- 《java入门第一季》二维数组三个案例详解
案例一:遍历二维数组 /* 需求:二维数组遍历 外循环控制的是二维数组的长度,其实就是一维数组的个数行数. 内循环控制的是一维数组的长度,每一行,一维数组元素分别的个数. */ class Array ...
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
FROM :http://www.sufeinet.com/forum.php?mod=viewthread&tid=1736&extra=page%3D1%26filter%3Dty ...
- java 编程性能调优
一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子: import java.util ...
- SharePoint2010搜索的简单设置
1. 开启搜索服务,管理中心 – 应用程序管理 – 服务应用程序 – 管理服务器上的服务 2. 点击进去,启动"SharePoint Foundation搜索"."S ...
- Android为什么使用Binder-android学习之旅(101)
基础知识 Android进程和linux进程一样,他们只运行在进程固有的虚拟空间中.一个4GB的虚拟地址空间,其中3GB是用户空间,1GB是内核空间 ,用户空间是非共享的,内核空间是共享的,如下图: ...
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- window 8.1 + python 3.6 + chrome 59 + selenium 3.4 环境配置
系统环境 window 8.1 python 3.6 (已经安装了pip) chrome 59.0.3071.115 步骤 安装selenium pip install selenium 下载chro ...
- MySQL基本sql语句
MySQL基本操作语句 操作文件夹(库) 增加create database 库名 charset utf8;charset utf8是指定库的字符编码删除drop database 库名删除某个数据 ...
- element.dispatchEvent is not a function的解决
Firebug中的出错提示: element.dispatchEvent is not a function element.dispatchEvent(event); prototype.js (第 ...
- JavaScript教程大纲
因为考虑到Python的接受难度,改为推广较为简单和流行的JavaScript.先列主要参考资料: JavaScript权威指南(第6版):http://book.douban.com ...