当前位置:网站首页>About mit6.828_ HW9_ Some problems of barriers xv6 homework9
About mit6.828_ HW9_ Some problems of barriers xv6 homework9
2022-07-28 16:39:00 【saddlesad】
Recently learning MIT6.828, Did HomeWork9:barriers.
The main idea is to implement a thread barrier , Multiple threads will call barrier() function , Then ask all threads to enter this barrier Function before leaving barrier.
My realization is like this :
/*nthread Is the total number of threads ,bstate.nthread Yes, the initial value is 0 A counter for */
static void barrier()
{
#define wait() pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex)
#define notify() pthread_cond_broadcast(&bstate.barrier_cond)
static int resume = 0; // Express wait1,wait2 The state of
pthread_mutex_lock(&bstate.barrier_mutex);
++bstate.nthread; // safe
notify();
while (bstate.nthread < nthread && !resume)
wait(); // wait1
resume = 1; // Avoid having threads execute --bstate.nthread Destroyed the triggered wait1 Conditions ,
--bstate.nthread;
if (bstate.nthread == 0)
bstate.round++;
notify();
while (bstate.nthread > 0 && resume)
wait(); // wait2
resume = 0; // Avoid having threads execute ++bstate.nthread Destroyed the triggered wait2 Conditions ,
pthread_mutex_unlock(&bstate.barrier_mutex);
#undef wait
#undef notify
}
But when I try to search for standard answers on the Internet , The realization of uniformity is like this :
static void barrier()
{
pthread_mutex_lock(&bstate.barrier_mutex);
bstate.nthread++;
if (bstate.nthread == nthread)
{
// The last process enters perform round++, Wake up other processes .
bstate.round++;
bstate.nthread = 0;
// Wake up other processes
pthread_cond_broadcast(&bstate.barrier_cond); // BBB
}
else
{
// Is there no false wake-up problem ?
pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex); // AAA
}
pthread_mutex_unlock(&bstate.barrier_mutex);
}
Undertested , Both implementations work properly .
however , I think there is a risk of false awakening in the implementation of online ( Humble opinion of Xiaobai , If there is any misunderstanding, please point out ):
- Under normal circumstances , If the total is 4 Threads , After the current three threads enter , They will all be blocked in AAA In the sentence , Until the fourth thread enters and executes BBB sentence , front 3 All threads will start from wait Wake up in , Then get the lock in turn to unlock and then exit
barrier(). - however , If the first three threads are blocked in AAA after , Threads 4 Not yet in
barrier()function , Then there is a false wake-up at the operating system level , Cause before 3 Threads from wait Back in , Then they will exit directlybarrier(), This violates the definition of barrier .
I hope to have some good advice .
边栏推荐
- PHP image synthesis technology
- PHP图片合成技术
- Ansa secondary development - two methods of drawing the middle surface
- The epidemic dividend disappeared, and the "home fitness" foam dissipated
- 重置grafana登录密码为默认密码
- Ansa secondary development - build ansa/meta secondary development environment on pycharm
- Ansa secondary development - build ansa secondary development environment on Visual Studio code
- 局域网无法访问apache服务器
- c语言编程当中两个!!的作用
- Detailed explanation of QT qstring
猜你喜欢

USB产品(FX3、CCG3PA)的调试方法

Ansa secondary development - build ansa secondary development environment on Visual Studio code

Headline article_ signature

mysql 查看事件状态语句和修改办法

HM二次开发 - Data Names及其使用

排序5-计数排序

Several methods of HyperMesh running script files

flashfxp 530 User cannot log in. ftp

Sort 1-insert sort and Hill sort

ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
随机推荐
HM二次开发 - Data Names及其使用
视频号找到金钥匙,抖音模仿后来人
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
8051 series MCU firmware upgrade IAP
PHP 图片上传
nowcode-学会删除链表中重复元素两题(详解)
HyperMesh运行脚本文件的几种方法
The video Number finds the golden key, and Tiktok imitates the latecomers
优化Hypermesh脚本性能的几点建议
ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境
PHP图片合成技术
Headline article_ signature
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Numpy ndarray learning < II > miscellaneous records
el-input限制只能输入规定的数
ANSA二次开发 - 抽中面的两种方法
ANSA二次开发 - Apps和ANSA插件管理
Leetcode topic
Configure HyperMesh secondary development environment on vs Code