NEU操作系统实验课4——线程同步
实验要求:
创建两个线程按数字顺序打印10以下自然数,其中一个线程打印1-3及8-10;另一个线程打印4-6。要求使用线程同步机制实现上述打印顺序。
看网上的资料学习了两种写法
C++11的,使用了std::thread
#include <unistd.h>
#include <thread>
#include <iostream>
#include <mutex>
#include<bits/stdc++.h>
void Fun_1();
void Fun_2();
unsigned int counter=;
std::mutex mtx; int main()
{
std::thread thrd_1(Fun_1);
std::thread thrd_2(Fun_2);
thrd_1.join();
thrd_2.join();
//system("pause");
return ;
} void Fun_1()
{
while(counter<){
if(counter<||counter>)continue;
std::lock_guard<std::mutex> mtx_locker(mtx);
++counter;
std::cout << "Function 1 counting " << counter << "...\n";
usleep();
}
} void Fun_2()
{
while(counter<){
if(counter>&&counter<)continue;
std::lock_guard<std::mutex> mtx_locker(mtx);
++counter;
std::cout << "Function 2 counting " << counter << "...\n";
usleep();
}
}
C++11
C语言的,使用pthread
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h> pthread_mutex_t mutex;
pthread_cond_t cond1;
pthread_cond_t cond2;
int cnt=; void hander(void *arg)
{
free(arg);
(void)pthread_mutex_unlock(&mutex);
} void *thread1(void *arg)
{
pthread_cleanup_push(hander,&mutex);
printf("thread1\n1\n2\n3\n");
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond1,&mutex);
pthread_mutex_unlock(&mutex);
printf("thread1\n7\n8\n9\n10\n");
pthread_cleanup_pop();
} void *thread2(void *arg)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond2,&mutex);
printf("thread2\n4\n5\n6\n");
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
} int main()
{
pthread_t tid1,tid2;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond1,NULL);
pthread_cond_init(&cond2,NULL);
pthread_create(&tid1,NULL,thread1,NULL);
pthread_create(&tid2,NULL,thread2,NULL); sleep();
pthread_cond_signal(&cond2);
//sleep(2);
pthread_exit();
return ;
}
C
NEU操作系统实验课4——线程同步的更多相关文章
- 线程同步CriticalSection
孙鑫 第十五/十六课之四 线程同步CriticalSection 说明 在使用多线程时,一般很少有多个线程完全独立的工作.往往是多个线程同时操作一个全局变量来获取程序的运行结果.多个线程同时访问同一个 ...
- 操作系统中的进程同步与Window中利用内核对象进行线程同步的关系
操作系统中为了解决进程间同步问题提出了用信号量机制,信号量可分为四种类型分别是互斥型信号量,记录型信号量,AND型信号量,信号量集. 互斥型信号量 互斥型信号量是资源数量为1的特殊的记录型信号量.表示 ...
- 分析.Net里线程同步机制
我 们知道并行编程模型两种:一种是基于消息式的,第二种是基于共享内存式的. 前段时间项目中遇到了第二种 使用多线程开发并行程序共享资源的问题 ,今天以实际案例出发对.net里的共享内存式的线程同步机制 ...
- Windows线程同步(上)
先介绍一个创建线程的API,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx ...
- Visual C++线程同步技术剖析:临界区,时间,信号量,互斥量
使线程同步 在程序中使用多线程时,一般很少有多个线程能在其生命期内进行完全独立的操作.更多的情况是一些线程进行某些处理操作,而其他的线程必须对其处理结果进行了解.正常情况下对这种处理结果的了解应当在其 ...
- ucore操作系统学习(七) ucore lab7同步互斥
1. ucore lab7介绍 ucore在前面的实验中实现了进程/线程机制,并在lab6中实现了抢占式的线程调度机制.基于中断的抢占式线程调度机制使得线程在执行的过程中随时可能被操作系统打断,被阻塞 ...
- 【C#进阶系列】28 基元线程同步构造
多个线程同时访问共享数据时,线程同步能防止数据损坏.之所以要强调同时,是因为线程同步问题实际上就是计时问题. 不需要线程同步是最理想的情况,因为线程同步一般很繁琐,涉及到线程同步锁的获取和释放,容易遗 ...
- windows核心编程 - 线程同步机制
线程同步机制 常用的线程同步机制有很多种,主要分为用户模式和内核对象两类:其中 用户模式包括:原子操作.关键代码段 内核对象包括:时间内核对象(Event).等待定时器内核对象(WaitableTim ...
- C#中的线程(二) 线程同步基础
1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具: 简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...
随机推荐
- Django模板常用语法规则
Django 模板标签 if/else 标签 for 标签 ifequal/ifnotequal 标签 注释标签 过滤器 include 标签 URL标签 模板继承 if/else 标签 1. 基 ...
- Beta冲刺-星期五
这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 Three cobblers 这个作业的目标 完成项目最后的冲刺 ...
- 书不在多,精读则灵 - Oracle入门书籍推荐
作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.com/archives/2006/08/ora ...
- C#使用wkhtmltopdf,把HTML生成PDF(包含分页)
最近花了2天多的时间终于把HTML生成PDF弄好了.步骤如下: 1.首先是技术选型.看了好多都是收费的就不考虑了. 免费的有: jsPDF(前端生成,清晰度不高,生成比较慢) iText(严格要求ht ...
- 【Oracle】RedHat 6.5 安装 11gR2数据库
1. 挂载操作系统光盘 [root@drz ~]# mount /dev/cdrom /mnt mount: block device /dev/sr0 is write-protected, mou ...
- VR: AR和VR演进哲学
Facebook 20亿美元(4亿美元+16亿美元股票换购方式)收购虚拟现实厂商Oculus 引爆AR产业,索尼不温不火逐步演进的头盔项目也该加速了.最近Oculus rift发布了商业版本:Ocul ...
- js 习题
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- C#第十二节课
数组 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Th ...
- 通过JDBC取Oracle数据库的时间字段时,时间丢失,只剩日期
通过JDBC连接Oracle数据库,在查询的时候发现时间字段取出来值后只剩下了日期,时间消失了.查资料发现跟Oracle jdbc驱动版本有关,这里先贴出解决方案: 修改数据库的连接方式: try { ...