- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以提供一些如何解决以下分配的示例/提示/指示:资源可以由两种类型的进程使用:黑色和白色。当白色进程使用该资源时,黑色进程就不能使用该资源,反之亦然。实现对资源的访问,避免饥饿。在一篇较旧的文章中,有人建议我使用 seqlock 算法的变体,但我不知道如何针对此分配调整该算法。
编辑:这是我到目前为止编写的代码
#include <stdio.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
struct RW;
struct RW
{
volatile int num_reads_in_progress;
volatile int num_writes;
pthread_cond_t reader_cv;
pthread_cond_t writer_cv;
pthread_mutex_t lock;
};
char *buf;
void signal_next(struct RW *b);
extern char *xx_read(struct RW *);
extern void xx_write(struct RW *, char *);
// Precondition: b->lock must be locked before this function is called
void signal_next(struct RW *b)
{
if (b->num_writes > 0)
{
// if any writes are waiting wake one up
pthread_cond_signal(&b->writer_cv);
}
else
{
// if are no writes pending, wake up all the readers
pthread_cond_broadcast(&b->reader_cv);
}
}
void *ts_read(void *vb);
void *ts_read(void *vb)
{
struct RW *b = vb;
pthread_mutex_lock(&b->lock);
while (b->num_writes > 0)
{
// cond_wait unlocks the mutex, waits to be signaled, then re-acquires the mutex
pthread_cond_wait(&b->reader_cv, &b->lock);
}
// By there b->num_writes must be 0
b->num_reads_in_progress++;
pthread_mutex_unlock(&b->lock);
buf = xx_read(b);
pthread_mutex_lock(&b->lock);
b->num_reads_in_progress--;
signal_next(b);
pthread_mutex_unlock(&b->lock);
return 0;
}
void *ts_write(void *vb);
void *ts_write(void *vb)
{
struct RW *b = vb;
pthread_mutex_lock(&b->lock);
b->num_writes++;
if (b->num_writes > 1 || b->num_reads_in_progress > 0)
{
// cond_wait unlocks the mutex, waits to be signaled,
// then re-acquires the mutex
pthread_cond_wait(&b->writer_cv, &b->lock);
}
pthread_mutex_unlock(&b->lock);
xx_write(b, buf);
pthread_mutex_lock(&b->lock);
b->num_writes--;
signal_next(b);
pthread_mutex_unlock(&b->lock);
return 0;
}
int main(void)
{
pthread_t white[3];
pthread_t black[3];
struct RW *rw;
rw = malloc(sizeof(struct RW));
int i;
for (i = 0; i < 3; i++)
{
pthread_create(&white[i], NULL, &ts_read, &rw);
}
for (i = 0; i < 3; i++)
{
pthread_create(&black[i], NULL, ts_write, &rw);
}
for (i = 0; i < 3; i++)
{
pthread_join(white[i], NULL);
}
for (i = 0; i < 3; i++)
{
pthread_join(black[i], NULL);
}
return 0;
}
最佳答案
您需要一个可以锁定和解锁的互斥体。基本上,您可以将互斥锁视为一个 bool 值,该值要么为真,要么为假(如果您愿意,可以锁定或解锁)。
当黑色进程访问资源时,互斥锁应该被锁定。另一方面,当白色尝试访问它时,它应该首先检查互斥锁的状态。如果互斥体的状态是锁定的,则必须等待互斥体解锁。
伪代码:
无符号字符互斥体 = 0;
//processBlack tries to access resource
if(mutex == 1)
while(mutex != 0);
mutex = 1;
//now the mutex is unlocked, do whatever you want
mutex = 0; //do not forget to unlock it.
//processWhite tries to access resource
if(mutex == 1)
while(mutex != 0);
mutex = 1;
//now the mutex is unlocked, do whatever you want
mutex = 0; //do not forget to unlock it.
关于c - seqlock 算法的变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20311288/
有人可以提供一些如何解决以下分配的示例/提示/指示:资源可以由两种类型的进程使用:黑色和白色。当白色进程使用该资源时,黑色进程就不能使用该资源,反之亦然。实现对资源的访问,避免饥饿。在一篇较旧的文章中
我正在研究Seqlock的实现.然而,我发现的所有来源都以不同的方式实现它们。 Linux内核 Linux kernel implements it like this : static inline
我创建了自己的简单、紧凑的 ReadWriteLock 实现。第一个在尝试获取读锁时使用自旋锁。如果设置了锁定位,则第二个通过在自旋之前暂时获取写锁来避免自旋锁。这样,它会停止执行,直到写锁被释放。现
另一个question让我想知道是否 seqlock可以使用 Java 中的 volatile 版本计数器有效地实现。 这是一个原型(prototype)实现,对于只有一个编写器线程的情况: clas
我想使用 C++11 原子库编写一个 seqlock。我在 stackoverflow 上看了一些关于 seqlock 的问题,但是没有人帮助我。我用的算法很普通,到处都能找到。这就是我的代码: st
我是一名优秀的程序员,十分优秀!