由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 出个题
相关主题
一道关于lock的面试题thread-safe 的 queue
怎么才是 精简,准确呢? spinlock VS semaphore 的 区别??多线程打印Message问题
请教一个java Synchronized Blocks和Lock的问题请教如何保证函数时thread safe的?
VMware onsitethread safe hash table
read-write locker的实现Code a non blocking thread safe queue
请教大牛用mutex lock实现reader writer lock问一道题 实现malloc
MS Azure组面试请问pure storage 的那道用spin lock and flags to implement mutex怎么做
求教一个今天被面到的多线程的问题问道面试题
相关话题的讨论汇总
话题: lock话题: filelock话题: writer
进入JobHunting版参与讨论
1 (共1页)
N*D
发帖数: 3641
1
给个Lock w/ two atomic method lock() and unlock(),请用lock实现一个文件读写的
系统,要求:
1: reader blocks writer;
2: writer blocks reader;
3: writer blocks writer;
x***y
发帖数: 633
2
int numOfReaders = 0;
int numOfWriters = 0;
int numOfWaitingWriters = 0;
Lock aLock;
void read(...){
bool hasNotRead = true;
while(hasNotRead){
aLock.lock();
if(numOfWriters==0 && numOfWaitingWriters==0){
numOfReaders++;
hasNotRead = flase;
}
aLock.unlock();
}
............//open file and read
aLock.lock();
numOfReaders--;
aLock.unlock();
}
void write(...){
aLock.lock();
numOfWaitingWriters++;
aLock.unlock();
bool hasNotWrite = true;
while(hasNotWrite){
aLock.lock();
if(numOfWriters==0 && numOfReaders==0){
numOfWriters++;
hasNotWrite = false;
}
aLock.unlock();
}
.........//write to the file
aLock.lock();
numOfWriters--;
numOfWaitingWriters--;
aLock.unLock();
}

c****o
发帖数: 41
3
My 2 cents. 多个reader可以同时读;但假如有多个reader读时来了一个writer,则之
后的
reader不能再读,直到writer结束。
读文件:
FileLock.Readlock();
//read ...
FileLock.ReadUnlock();
写文件:
FileLock.WriteLock();
//write ...
FileLock.WriteUnlock();
class FileLock 如下:
class FileLock
{
private:
Lock m_ReadCounterLock;
Lock m_FileLock;
Lock m_writerWaitLock;
int m_ReaderCount;
public:
void ReadLock()
{
m_ReadCounterLock.lock();

// 如果有writer等,block
m_writerWaitLock.lock();
m_writerWaitLock.unlock();
if(m_ReaderCount == 0)
{
m_FileLock.lock();
}
m_ReaderCount ++;
m_ReadCounterLock.unlock();
}
void ReadUnlock()
{
m_ReadCounterLock.lock();

m_ReaderCount --;
if(m_ReaderCount == 0)
{
m_FileLock.unlock();
}
m_ReadCounterLock.unlock();
}
void WriteLock()
{
// lock m_writerWaitLock,之后的reader必须等
m_writerWaitLock.lock();
m_FileLock.lock();
}

void WriterUnlock()
{
m_FileLock().unlock();
m_writerWaitLock.unlock();
}
}



【在 N*D 的大作中提到】
: 给个Lock w/ two atomic method lock() and unlock(),请用lock实现一个文件读写的
: 系统,要求:
: 1: reader blocks writer;
: 2: writer blocks reader;
: 3: writer blocks writer;

N*D
发帖数: 3641
4
这个Lock用法不对呀,你只是用Lock保证了对Counter的Atomic访问,对于文件的读写
没有Lock呀。
比如先来个Writer,正写文件呢,前面对counter的update的lock都release了,又来个
writer,也进入Lock去,if statement一判断,啥也不用干,release lock,it也写文
件去了。呵呵。

int numOfReaders = 0;
int numOfWriters = 0;
int numOfWaitingWriters = 0;
Lock aLock;
void read(...){
bool hasNotRead = true;
while(hasNotRead){
aLock.lock();
if(numOfWriters==0 && numOfWaitingWriters==0){
numOfReaders++;
hasNotRead = flase;
}
aLock.unlock();
}
............//open file and read
aLock.lock();
numOfReaders--;
aLock.unlock();
}
void write(...){
aLock.lock();
numOfWaitingWriters++;
aLock.unlock();
bool hasNotWrite = true;
while(hasNotWrite){
aLock.lock();
if(numOfWriters==0 && numOfReaders==0){
numOfWriters++;
hasNotWrite = false;
}
aLock.unlock();
}
.........//write to the file
aLock.lock();
numOfWriters--;
numOfWaitingWriters--;
aLock.unLock();
}



【在 x***y 的大作中提到】
: int numOfReaders = 0;
: int numOfWriters = 0;
: int numOfWaitingWriters = 0;
: Lock aLock;
: void read(...){
: bool hasNotRead = true;
: while(hasNotRead){
: aLock.lock();
: if(numOfWriters==0 && numOfWaitingWriters==0){
: numOfReaders++;

N*D
发帖数: 3641
5
非常接近了,不过Readlock里面对于writerWaitLock用法有些问题。lock总是为了lock
些什么东西,那个地方啥也没干,就是check一下,假设check当时没有waiting write
,check一结束就来了一个write呢?两个人就一块儿读写文件了。

My 2 cents. 多个reader可以同时读;但假如有多个reader读时来了一个writer,则之
后的
reader不能再读,直到writer结束。
读文件:
FileLock.Readlock();
//read ...
FileLock.ReadUnlock();
写文件:
FileLock.WriteLock();
//write ...
FileLock.WriteUnlock();
class FileLock 如下:
class FileLock
{
private:
Lock m_ReadCounterLock;
Lock m_FileLock;
Lock m_writerWaitLock;
int m_ReaderCount;
public:
void ReadLock()
{
m_ReadCounterLock.lock();

// 如果有writer等,block
m_writerWaitLock.lock();
m_writerWaitLock.unlock();
这个没啥意义,lock马上unlock,这个时候正好来个writer咋办?
if(m_ReaderCount == 0)
{
m_FileLock.lock();
}
m_ReaderCount ++;
m_ReadCounterLock.unlock();
}
void ReadUnlock()
{
m_ReadCounterLock.lock();

m_ReaderCount --;
if(m_ReaderCount == 0)
{
m_FileLock.unlock();
}
m_ReadCounterLock.unlock();
}
void WriteLock()
{
// lock m_writerWaitLock,之后的reader必须等
m_writerWaitLock.lock();
m_FileLock.lock();
}

void WriterUnlock()
{
m_FileLock().unlock();
m_writerWaitLock.unlock();
}
}


【在 c****o 的大作中提到】
: My 2 cents. 多个reader可以同时读;但假如有多个reader读时来了一个writer,则之
: 后的
: reader不能再读,直到writer结束。
: 读文件:
: FileLock.Readlock();
: //read ...
: FileLock.ReadUnlock();
: 写文件:
: FileLock.WriteLock();
: //write ...

x***y
发帖数: 633
6
If the first writer is writing, numOfWriters is not 0, then the 2nd writer
will be stuck in while loop and won't be able to write file; maybe not
efficient, but should work.

【在 N*D 的大作中提到】
: 这个Lock用法不对呀,你只是用Lock保证了对Counter的Atomic访问,对于文件的读写
: 没有Lock呀。
: 比如先来个Writer,正写文件呢,前面对counter的update的lock都release了,又来个
: writer,也进入Lock去,if statement一判断,啥也不用干,release lock,it也写文
: 件去了。呵呵。
:
: int numOfReaders = 0;
: int numOfWriters = 0;
: int numOfWaitingWriters = 0;
: Lock aLock;

c****o
发帖数: 41
7

lock
write
这种情况下,reader和writer会继续竞争m_FileLock, 只有一个能拿到。

【在 N*D 的大作中提到】
: 非常接近了,不过Readlock里面对于writerWaitLock用法有些问题。lock总是为了lock
: 些什么东西,那个地方啥也没干,就是check一下,假设check当时没有waiting write
: ,check一结束就来了一个write呢?两个人就一块儿读写文件了。
:
: My 2 cents. 多个reader可以同时读;但假如有多个reader读时来了一个writer,则之
: 后的
: reader不能再读,直到writer结束。
: 读文件:
: FileLock.Readlock();
: //read ...

j*****u
发帖数: 1133
8
给点提示?
lock没有count,不是semaphore,也不是event,怎么做到all readers/one writer
finish了unblock one writer, all writer finish了 unblock all readers?

写的

【在 N*D 的大作中提到】
: 给个Lock w/ two atomic method lock() and unlock(),请用lock实现一个文件读写的
: 系统,要求:
: 1: reader blocks writer;
: 2: writer blocks reader;
: 3: writer blocks writer;

1 (共1页)
进入JobHunting版参与讨论
相关主题
问道面试题read-write locker的实现
Google 电面面经请教大牛用mutex lock实现reader writer lock
几个面试问题供讨论MS Azure组面试
问个算法题求教一个今天被面到的多线程的问题
一道关于lock的面试题thread-safe 的 queue
怎么才是 精简,准确呢? spinlock VS semaphore 的 区别??多线程打印Message问题
请教一个java Synchronized Blocks和Lock的问题请教如何保证函数时thread safe的?
VMware onsitethread safe hash table
相关话题的讨论汇总
话题: lock话题: filelock话题: writer