pthread2
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
int myglobal;
void * thread_function(void * arg)
{
int i,j;
for (i=0; i<20; i++) {
j=myglobal;
j++;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
}
return NULL;
}
int main(int argc, const char * argv[])
{
pthread_t mythread;
int i;
if (pthread_create(&mythread, NULL, thread_function, NULL)) {
printf("error creating thread");
abort();
}
for (i=0; i<20; i++) {
myglobal++;
printf("o");
fflush(stdout);
sleep(1);
}
if (pthread_join(mythread, NULL)) {
printf("error joining thread");
abort();
}
printf("\nmyglobal equals %d\n",myglobal);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
void * thread_function(void * arg)
{
int i,j;
for (i=0; i<20; i++) {
pthread_mutex_lock(&mymutex);
j=myglobal;
j++;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
pthread_mutex_unlock(&mymutex);
}
return NULL;
}
int main(int argc, const char * argv[])
{
pthread_t mythread;
int i;
if (pthread_create(&mythread, NULL, thread_function, NULL)) {
printf("error creating thread");
abort();
}
for (i=0; i<20; i++) {
pthread_mutex_lock(&mymutex);
myglobal++;
pthread_mutex_unlock(&mymutex);
printf("o");
fflush(stdout);
sleep(1);
}
if (pthread_join(mythread, NULL)) {
printf("error joining thread");
abort();
}
printf("\nmyglobal equals %d\n",myglobal);
return 0;
}
对于线程的运行我们一定不要假设哪个会在前面运行哪个会在后面运行,只要线程创建以后我们可以认为他们是同时运行的。
我们看下互斥对象的使用。
首先是初始化
静态初始化给变量赋一个常数
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
另外也可以动态的创建互斥对象,即像使用malloc一样动态分配,在这边我们使用
int pthread_mutex_init(pthread_mutex_t *restrict, const pthread_mutexattr_t *restrict)
第一个参数是要初始化的互斥对象,这个指针已经分配好了一块内存,第二个参数是设定互斥量属性的,为空则为默认属性。
如果使用了pthread_mutex_init初始化了互斥对象,就要用pthread_mutex_destory来销毁,其参数为指向要销毁的互斥量。注意的是这边不会释放用来存储互斥量pthread_mutex_t的内存,也就是我们可以直接再用pthread_mutex_init来重新初始化被销毁的内存。
另外我们要注意的是不管是创建还是销毁,成功的时候返回的都是0.
使用
pthread_mutex_lock接受一个互斥对象的指针作为参数,将其锁定,如果该互斥对象已经锁定,则该调用者进入睡眠状态,如果函数返回,则唤醒当前线程。
pthread_mutex_unlock与上述配合使用,看到名字我们知道这个是解锁。
这里我们要明确的是一定要尽快对已经加锁的互斥对象进行解锁,以提高性能。
另外一定不要对没有加锁的互斥对象进行解锁,这样pthread_mutex_unlock会调用失败。
我们会考虑下一个问题就是,计算机要不停的监测这个互斥量的状态,改变状态之后要立即做出反应,这样是不可取的,因此也就是有了信号量这一说。
通过信号量来使线程进入睡眠状态或者唤醒这个线程。
pthread2的更多相关文章
- volatile关键字及编译器指令乱序总结
本文简单介绍volatile关键字的使用,进而引出编译期间内存乱序的问题,并介绍了有效防止编译器内存乱序所带来的问题的解决方法,文中简单提了下CPU指令乱序的现象,但并没有深入讨论. 以下是我搭建的博 ...
- GDB深入研究——20135308芦畅
GDB深入研究 一.GDB代码调试 (一)GDB调试实例 在终端中编译一个示例C语言小程序,保存到文件 gdb-sample.c 中,用GCC编译之 #include <stdio.h> ...
- GDB深入研究
GDB深入研究 一.GDB代码调试 (一)GDB调试实例 在终端中编译一个示例C语言小程序,保存到文件 gdb-sample.c 中,用GCC编译之 #include <stdio.h> ...
- Linux进程间通信与线程间同步详解(全面详细)
引用:http://community.csdn.net/Expert/TopicView3.asp?id=4374496linux下进程间通信的几种主要手段简介: 1. 管道(Pipe)及有名管道( ...
- 【转】 Linux 线程同步的三种方法
线程的最大特点是资源的共享性,但资源共享中的同步问题是多线程编程的难点.linux下提供了多种方式来处理线程同步,最常用的是互斥锁.条件变量和信号量. 一.互斥锁(mutex) 通过锁机制实现线程间的 ...
- 【转】 Linux下的多线程编程
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/07/280 ...
- Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- pthread_join和pthread_detach的用法(转)
一:关于join join join是三种同步线程的方式之一.另外两种分别是互斥锁(mutex)和条件变量(condition variable). 调用pthread_join()将阻塞自己,一直到 ...
- Linux系统编程@多线程编程(二)
线程的操作 线程标识 线程的ID表示数据类型:pthread_t (内核中的实现是unsigned long/unsigned int/指向pthread结构的指针(不可移植)几种类型) 1.对两个线 ...
随机推荐
- python网络爬虫之图片链家在技术.seleninum和PhantonJS
一.什么是图片懒加载? 案例分析:抓取站长素材http://sc.chinaz.com/中的图片数据 #!/usr/bin/env python # -*- coding:utf-8 -*- impo ...
- 题解报告:hdu 1213 How Many Tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- Elasticsearch之CURL命令的GET
这是个查询命令. 前期博客 Elasticsearch之CURL命令的PUT和POST对比 1. 以上是根据员工id查询. 即在任意的查询字符串中添加pretty参数,es可以得到易于我们识别的jso ...
- VS插件-Resharper
最近代码因为Resharper出现了点问题,同事问我这个插件有什么用,下面就列几个最近常用的功能.其他功能后续慢慢更新 1.什么是Resharper ReSharper是一个JetBrains公司出品 ...
- Sublime Text2安装emmet
一.安装Package Control 如果Preferences中没有Package Control,需要手动安装.安装方法如下: 访问Package Controls站点复制一段python命令安 ...
- ionic start 又一次的出错
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_modules\ ...
- 初始MongoDB------MongoDB的安装
MongoDB在Windows的安装是很简单的,无论是安装包还是绿色包,安装出来的都是这些文件 重点是Bin中的东西 特别是前两个的执行文件 mongod进程就是启动MongoDB数据库的进程 ...
- 计组_IEEE754_练习题
IEEE754 阶码:移码:尾数:原码 一个规格化的32位浮点数x的真值可表示为: x=(-1)^s×(1. M) × 2^(E-127) e=E-127 其中尾数域 ...
- 转录组入门(3):了解fastq测序数据
sra文件转换为fastq格式 fastq-dump -h --split-3 也就是说如果SRA文件中只有一个文件,那么这个参数就会被忽略.如果原文件中有两个文件,那么它就会把成对的文件按*_1.f ...
- Linux命令(文本编辑器)
vi和vim编辑器:有插入模式,一般模式,地行模式 一班模式通过(i.a.o.I.A.O)键--->进入插入模式 插入模式(按Esc键退出)---->j进入一班模式 ...