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;
|