- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试解决我们的操作系统教授在上一次考试中向我们展示的问题,以便为下一次考试做准备。
问题是有两个线程同时执行并且可能在不同的时间内完成。一个特定的线程完成后,它需要阻塞直到另一个线程完成,然后它们才能继续执行。
这在我看来在概念上很简单,但我的代码并没有按照我认为的方式工作。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#define N 10
sem_t t_1_sem;
sem_t t_2_sem;
void *thread(void *vargp);
/* shared by both threads*/
struct {
int count;
} thread_count;
int main() {
pthread_t tid, tid1;
thread_count.count = 0;
sem_init(&t_1_sem, 0, 1);
sem_init(&t_2_sem, 0, 1);
printf("Hello from main thread! tid:%ld pid:%d\n", pthread_self(), getpid());
pthread_create(&tid, NULL, thread, NULL);
pthread_create(&tid1, NULL, thread, NULL);
pthread_join(tid, NULL);
pthread_join(tid1, NULL);
exit(0);
}
void *thread(void *vargp) {
int i, tid;
int val, val2;
sem_getvalue(&t_1_sem, &val);
sem_getvalue(&t_2_sem, &val2);
printf("initial value::: %d : %d\n", val, val2);
tid = thread_count.count;
thread_count.count += 1;
for(i = 0;i<N;i++){
printf("%d, %d\n", tid, i);
fflush(stdout);
//sleep(0.1);
}
// TODO
// barrier
sem_getvalue(&t_1_sem, &val);
sem_getvalue(&t_2_sem, &val2);
printf("second value::: %d : %d\n", val, val2);
int sem_val;
if(tid == 0){
// free other
sem_getvalue(&t_1_sem, &sem_val);
printf("posting to 2, waiting on 1 w/ %d count\n", sem_val);
sem_post(&t_2_sem);
// wait on this one
sem_wait(&t_1_sem);
printf("done waiting on 1\n");
} else if(tid == 1){
sem_getvalue(&t_2_sem, &sem_val);
printf("posting to 1, waiting on 2 w/ %d count\n", sem_val);
sem_post(&t_1_sem);
sem_wait(&t_2_sem);
printf("done waiting on 2\n");
}
sem_getvalue(&t_1_sem, &val);
sem_getvalue(&t_2_sem, &val2);
printf("final value::: %d : %d\n", val, val2);
return NULL;
}
我期望看到的是两个线程都计数到 10,然后两个“最终值”printf
彼此相邻。但是,我看到的是在线程完成计数到 10 后立即打印“最终值”——它似乎没有等待。
我在“发布到 N”printf
中打印的 sem_val
整数也得到了非常奇怪的值,例如:
Hello from main thread! tid:-1606277344 pid:5479
initial value::: 0 : 0
0, 0
initial value::: 0 : 0
1, 0
0, 1
1, 1
0, 2
1, 2
1, 3
1, 4
1, 5
0, 3
1, 6
0, 4
1, 7
0, 5
1, 8
0, 6
1, 9
0, 7
second value::: 0 : 0
posting to 1, waiting on 2 w/ -1809628646 count
0, 8
done waiting on 2
final value::: 0 : 0
0, 9
second value::: 0 : 0
posting to 2, waiting on 1 w/ -1809628646 count
done waiting on 1
final value::: 0 : 0
有什么想法/提示吗?
最佳答案
可能要咨询The Little Book of Semaphores . 3.5 节描述了 Barrier 模式及其正确实现方式。
我知道这并不能直接回答您的问题,但它应该能为您指明正确的方向。
关于C 信号量和线程之间的 "barrier",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1569826/
我是 Barrier-kvm 的常客。我最近将我的服务器升级到 Ubuntu 20.04 并开始收到错误“错误:ssl 证书不存在:/home/rsvay/snap/barrier-kvm/2/.lo
根据维基百科:内存屏障,也称为 membar、内存栅栏或栅栏指令,是一种栅栏指令,它导致中央处理单元 (CPU) 或编译器对在屏障指令。 这通常意味着在屏障之前发出的操作保证在屏障之后发出的操作之前执
我正在尝试使用omp来实现listranking问题(也称为快捷方式),以使数组W的和为前缀。 我不知道我是否正确使用了冲洗编译指示。 而且我在编译时发出警告:“障碍区域可能不会紧密嵌套在工作共享,关
问题 是否允许在发散流控制之后但在发散流控制之外使用 barrier()? 详细信息 在兼容 OpenGL 4.00 的计算着色器中,我正在做一些涉及发散(即非动态统一)分支语句的工作。稍后在同一个着
我想制作一个看起来像这样的布局,但使用 Barrier: 所以我制作了以下 XML: 但是结果是这样的: Barrier 似乎没有向下移动。我做错了什么?
我了解了一些关于 GCD 障碍的知识并想检查这些信息(来自 Apple docs ): Any blocks submitted after the barrier block are not exe
通常,线程屏障(即 boost::barrier)用一个整数初始化,该整数表示必须调用 boost::barrier::wait 的线程数 - 所有线程都在该点等待,直到满足条件,然后所有线程继续。
我正在尝试解决我们的操作系统教授在上一次考试中向我们展示的问题,以便为下一次考试做准备。 问题是有两个线程同时执行并且可能在不同的时间内完成。一个特定的线程完成后,它需要阻塞直到另一个线程完成,然后它
我正在研究测试驱动开发,其中一个讨论点是与 TDD 相关的“进入壁垒”。有没有人在这方面有任何经验,在您参与的任何项目中,由于进入阈值太高而决定不使用 TDD? 据我所知,进入的唯一障碍是个人开发人员
如何使用类成员函数初始化 std::barrier ? class CMyClass { private: void func() { } public: void start
作为类(class)作业的一部分,我必须使用锁来实现自定义屏障类。为了测试我的 LockBarrier 类,我想出了以下测试代码。它工作正常,但我担心这是否是正确的方法。您能否建议我可以做的改进,特别
最近在看页面The JSR-133 Cookbook for Compiler Writers由 Doug Lea 关于 JSR 133: JavaTM Memory Model and Thread
我想在我的 cpp 多线程代码中使用 std::experimental::barrier。但即使我写这样的代码: #include #include #include int main ()
我目前正在使用 MPI C 库,但是使用 C++ 编码,我知道 MPI_Barrier(MPI_COMM_WORLD) 函数会阻止调用者,直到通信器中的所有进程都调用它 , 如 documentati
在omp临界区之后是否存在隐式omp屏障 例如,我可以将以下代码版本 1 修改为版本 2 吗? 版本-1 int min = 100; #pragma omp parallel { int lo
我不确定这是否在 xcode8 beta 5 中崩溃了。看看这段代码。你认为它应该先打印“A”还是先打印“B”? let q = DispatchQueue(label: "q", attribute
在 Java 中,当我们有两个线程共享以下变量时: int a; volatile int b; 如果线程 1 执行: a = 5; b = 6; 然后在这两条指令之间插入一个 StoreStore
从 Linux 内核代码中,我可以看到 preempt_enable() 和 preempt_disable() 除了 barrier() 之外什么都没有: #define preempt_disab
我有时会在有关内存排序的教程中看到“完全内存屏障”一词,我认为这意味着: 如果我们有以下指示: instruction 1 full_memory_barrier instruction 2 然后,不
我已经阅读了我能找到的所有关于 torch.distributed.barrier() 的文档,但仍然无法理解它在 this script 中的使用方式并且非常感谢一些帮助。 所以official d
我是一名优秀的程序员,十分优秀!