mutex,thread
//#include <stdio.h>
//#include <stdlib.h>
//#include <unistd.h>
#include <windows.h>
//#include <pthread.h>
#include <mutex>
#include <thread>
#include <string.h>
using namespace std;
char* buf[5]; //字符指针数组 全局变量
int pos; //用于指定上面数组的下标
//1.定义互斥量
//pthread_mutex_t mutex; // linux
mutex mtx; // C++11
HANDLE g_hMutex = INVALID_HANDLE_VALUE; // Win
void *task(void *p)
{
//3.使用互斥量进行加锁
//pthread_mutex_lock(&mutex); // linux
//mtx.lock(); // C++11
WaitForSingleObject(g_hMutex, INFINITE);//等待互斥量 Win
buf[pos] = (char *)p;
printf("task %d\r\n", pos);
//sleep(1); // linux
Sleep(500); // Win
pos++;
//4.使用互斥量进行解锁
//pthread_mutex_unlock(&mutex); // linux
//mtx.unlock(); // C++11
ReleaseMutex(g_hMutex);//释放互斥量 // Win
return 0;
}
int main(void)
{
//2.初始化互斥量, 默认属性
//pthread_mutex_init(&mutex, NULL); // linux
g_hMutex = CreateMutex(nullptr, false, nullptr); // Win
//1.启动一个线程 向数组中存储内容
//pthread_t tid, tid2; // linux
//pthread_create(&tid, NULL, task, (void *)"woainia!"); // linux
//pthread_create(&tid2, NULL, task, (void *)"how"); // linux
// CreateThread(nullptr,0...) // Win
thread th1(task, (void *)"woainia!"); // C++
thread th2(task, (void *)"how"); // C++
//2.主线程进程等待,并且打印最终的结果
//pthread_join(tid, NULL); // linux
//pthread_join(tid2, NULL); // linux
th2.join(); // C++
th1.join(); // C++
//5.销毁互斥量
//pthread_mutex_destroy(&mutex); // linux
CloseHandle(g_hMutex); // Win
Sleep(5000);
int i = 0;
printf("字符指针数组中的内容是:pos:%d \r\n", pos);
for (i = 0; i < pos; ++i)
{
printf("%d : %s \n", i, buf[i]);
}
printf("\n");
system("pause");
return 0;
}

mutex,thread的更多相关文章
- mutex,thread(c++11 windows linux三种方式)
一 c++11 windows linux三种方式 //#include <stdio.h> //#include <stdlib.h> //#include <uni ...
- Thread and shared lock
在看programing python 4th,第5张parallel system tool 192页开始,书中讲到thread知识,以下做个笔记,以便后期学习 1.主线程执行,开启5个子线程进行计 ...
- mutex 互斥量
有用参考:http://blog.csdn.net/yl2isoft/article/details/46003467 摘抄记录:using System.Threading; class Examp ...
- 线程同步方式之互斥量Mutex
互斥量和临界区非常相似,只有拥有了互斥对象的线程才可以访问共享资源,而互斥对象只有一个,因此可以保证同一时刻有且仅有一个线程可以访问共享资源,达到线程同步的目的. 互斥量相对于临界区更为高级,可以对互 ...
- C# 多线程系列之Mutex使用
互斥量是一个内核对象,它用来确保一个线程独占一个资源的访问,并且互斥量可以用于不同进程中的线程互斥访问资源. 我们可以把Mutex看作一个出租车,乘客看作线程.乘客首先等车,然后上车,最后下车.当一个 ...
- c++11多线程---线程锁(mutex)
#include<mutex> 包含四类锁: 1 std::mutex 最基本也是最常用的互斥类 2 std::recursive_mutex 同一线程内可递归 ...
- <<面向模式的软件架构2-并发和联网对象模式>>读书笔记
服务访问和配置模式 Wrapper Facade可以将有非对象API提供的函数和数据封装到面向对象的类接口中 就是把底层API再封装一次,让外部不用关心是调用哪个平台的API,不如锁,在不同的平台上可 ...
- 并发编程中.net与java的一些对比
Java在并发编程中进行使用java.util.concurrent.atomic来处理一些轻量级变量 如AtomicInteger AtomicBoolean等 .Net中则使用Interlocke ...
- perl多线程理解
Thread:在使用多线程处理比较大的数据量的扫描,遇到读写文件可能死锁的问题. Perl 线程的生命周期 1.使用 threads 包的 create() 方法: use threads; sub ...
随机推荐
- 解决'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
懒癌晚期,直接贴图 然后就解决了!
- Linux和window的区别
免费与收费 最新正版Windows10官方售价¥888 Linux几乎免费(更多人愿意钻研开源软件,而收费的产品出现更多的盗版) 软件与支持 Windows平台:数量和质量的优势,补过大部分为收费软件 ...
- [LeetCode] Encode N-ary Tree to Binary Tree 将N叉树编码为二叉树
Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...
- [Codeforces Round #438][Codeforces 868C. Qualification Rounds]
题目链接:868C - Qualification Rounds 题目大意:有\(n\)个题目,\(k\)个人,每个人可能做过这\(n\)个题里的若干道,出题方要在这\(n\)个题目里选若干个出来作为 ...
- python语法_集合
集合:不同的元素(不可hash)组合在一起的就叫做集合,去掉重复的,以空字符返回,无序的 可以分为可变集合和不可变集合(frozenset) 创建: s = set('gm gyx') print(s ...
- F#周报2019年第14期
新闻 发布F# 4.6 SAFE Stack v1.0 发布fable编译器2.2,Fable.Core 3及其它 发布ML.NET 1.0 RC Saturn:增加路由诊断页面 Visual Stu ...
- 现代IM系统中消息推送和存储架构的实现
现代IM系统中消息推送和存储架构的实现-云栖社区-阿里云 https://yq.aliyun.com/articles/253242
- 学习h264 的语法规则,如何才能看懂H264 的官方文档
1. 今天想查h264 的帧率,查找资料如下: 首先要解析sps,得到两个关键的数值: num_units_in_tick, time_scale fps=time_scale/num_units_i ...
- 插值代码17个---MATLAB
函数名 功能Language 求已知数据点的拉格朗日插值多项式Atken 求已知数据点的艾特肯插值多项式Newton 求已知数据点的均差形式的牛顿插值多项式Newtonforward 求已知数据点的前 ...
- Java集合List、Set、Map
集合是 java 基础中非常重要的一部分,同样也是 Java 面试中很重要的一个知识点.所以,给王小整理了这篇关于集合的文章. 1.接口继承关系以及实现 集合类存放于 Java.util 包中,主要有 ...