我的程序现在要实现下列功能,可是我的确找不到实现方法:
1.两个缓冲区buffer1,buffer2,因为我想加快读与写之间的切换速度
所以选择至少两个buffer
2.程序只能实现读与写的互斥,而不能使得两个读者之间的互斥
3.要实现buffer1与buffer2的读写独立性,也即读者在读buffer1时,写者不能等待读buffer1的完成才写buffer2,否则条件1的快速切换不能满足,退化为只有一个buffer开始我想用 CEvent,接着我想用CCriticalSection,可想来想去都觉得不能满足我的要求.
 
请senoir们指点!!!谢谢!
--------------------------------------------
没有csdn,我的入门是不可想象的!

解决方案 »

  1.   

    想得挺复杂的,觉得有了两个读,又不互斥,可以省掉一个buffer吗。
      

  2.   

    多用几个CEvent是可以满足你的要求的啊,要用到多线程了
      

  3.   

    参考一下《windows核心编成》吧,上面有一个例子SWMR(single writer multi reader)就处理你的问题。
      

  4.   

    你的兩個buffer是有讀寫獨立性的,用兩套互斥變量給它們分別實現讀寫的互斥,互不牽連.
    這樣的讀寫互斥,一個buffer和兩個buffer有區別嗎?
      

  5.   

    我是这样想的:
    如果buffer1正在被读,那么如果只用一个buffer的话
    写者就要等待,是吗?这时如果能让写者写另一个buffer的话,那么自然可以加快
    程序运行速度
    如果不能轮换运行,实际上定义一个buffer就足以
      

  6.   


    对写者可以这样假设?
    ...
    start reader1(suspended)
    ...
    start reader2(suspended)
    ...
    while(bRuning)
    {
     if(getControll(buffer1)||getControll(buffer2))
    {
      if(getControll(buffer1)) //如果此时buffer1被读,不能阻塞写者,是吗?要马上判 
                              //断buffer2是否被读
      {   lock;
        filling(buffer1);
        unlock;
       wakeUp(reader);
       }
    if(getControll(buffer2))
    {
       lock;
    filling(buffer2);
     unlock
      wakeup(reader);
    }
    }
    }
      

  7.   

    使用WaitForMultipleObjects, 任一buffer可用, 你都可以知道.
      

  8.   

    寫得很亂,參考一下.#include <afxmt.h>char buf1[16] = {0}, buf2[16] = {0};
    // use to control accessing the buf
    CCriticalSection g_csBuf1, g_csBuf2;
    // use to set someone is accssing
    CEvent g_eventBuf1( TRUE, TRUE ), g_eventBuf2( TRUE, TRUE );
    int g_nReadingBuf1 = 0, g_nReadingBuf2 = 0; 
    // use to control accessing g_nReadingBuf1 and g_nReadingBuf2
    CCriticalSection g_csReaderCount1, g_csReaderCount2;DWORD WINAPI Reader1( LPVOID )
    {
        while (1)
        {
    // lock for g_nReadingBuf1
    g_csReaderCount1.Lock();
    g_nReadingBuf1++;
    // whether the first reader to access buf1
    if (g_nReadingBuf1 == 1)
    {
        g_csReaderCount1.Unlock();
        // enter the buf1 critical section
        g_csBuf1.Lock();
        // reading, no let writer accss buf1
        g_eventBuf1.ResetEvent();
    }
    else
        g_csReaderCount1.Unlock(); // access buf1
    char c = buf1[0]; g_csReaderCount1.Lock();
    g_nReadingBuf1--;
    // whether the last reader leaving buf1
    if (g_nReadingBuf1 == 0)
    {
        // let writer accss buf1
        g_eventBuf1.SetEvent();
        // leave the buf1 critical section
        g_csBuf1.Unlock();
    }
    g_csReaderCount1.Unlock();
        }    return 0;
    }// the same with Reader1
    DWORD WINAPI Reader2( LPVOID )
    {
        while (1)
        {
            g_csReaderCount2.Lock();
            g_nReadingBuf2++;
            if (g_nReadingBuf2 == 1)
            {
       g_csReaderCount2.Unlock();
       g_csBuf2.Lock();
       g_eventBuf2.ResetEvent();
            }
            else
       g_csReaderCount2.Unlock();        // access buf2
            char c = buf2[0];        g_csReaderCount2.Lock();
            g_nReadingBuf2--;
            if (g_nReadingBuf2 == 0)
            {
       g_eventBuf2.SetEvent();
       g_csBuf2.Unlock();
            }
            g_csReaderCount2.Unlock();
        }    return 0;
    }
    DWORD WINAPI Writer( LPVOID )
    {
        HANDLE pEvent[2] = { g_eventBuf1, g_eventBuf2 };    while (1)
        {
    DWORD ret = WaitForMultipleObjects( 2, pEvent, TRUE, INFINITE );
    if (ret == WAIT_OBJECT_0)  // buf1 is available
    {
        CSingleLock lock( &g_csBuf1 );
        // wait 0 
        lock.Lock( 0 );
        // if reader get buf1 again, continue to wait for both buf
        if (lock.IsLocked()) 
        {
    // writing, let other writer wait
    g_eventBuf1.ResetEvent(); // write buf1
    buf1[0] = '1'; g_eventBuf1.ResetEvent();
    lock.Unlock();
        }

    }
    else if (ret == WAIT_OBJECT_0 + 1)  // buf2 is available
    {
        CSingleLock lock( &g_csBuf2 );
        // wait 0 
        lock.Lock( 0 );
        // if reader get buf2 again, continue to wait for both buf
        if (lock.IsLocked())  
        {
    // writing, let other writer wait
    g_eventBuf2.ResetEvent(); // write buf2
    buf2[0] = '2'; g_eventBuf2.SetEvent();
    lock.Unlock();
        }
    }
        }    return 0;
    }
      

  9.   

    我要问 aachenG 
    1.reader1 也要读buffer2,对reader2同样道理,要读buffer1是吗?
    2.任何一个reader在读任何一个buffer的时候只能是读写的互斥,而不是读者的互斥,是吗?
    我会继续研究你的实现方法的!谢谢!多多关照!
      

  10.   

    reader中這一段有誤,一時糊塗了:
    // lock for g_nReadingBuf1
    g_csReaderCount1.Lock();
    g_nReadingBuf1++;
    // whether the first reader to access buf1
    if (g_nReadingBuf1 == 1)
    {
        g_csReaderCount1.Unlock();
        // enter the buf1 critical section
        g_csBuf1.Lock();
        // reading, no let writer accss buf1
        g_eventBuf1.ResetEvent();
    }
    else
        g_csReaderCount1.Unlock();應改為:
    // lock for g_nReadingBuf1
    g_csReaderCount1.Lock();
    g_nReadingBuf1++;
    // whether the first reader to access buf1
    if (g_nReadingBuf1 == 1)
    {
        // enter the buf1 critical section
        g_csBuf1.Lock();
        // reading, no let writer accss buf1
        g_eventBuf1.ResetEvent();
    }
    g_csReaderCount1.Unlock();
      

  11.   

    // lock for g_nReadingBuf1
    g_csReaderCount1.Lock();
    g_nReadingBuf1++;
    // whether the first reader to access buf1
    if (g_nReadingBuf1 == 1)
    {
        // enter the buf1 critical section
        g_csBuf1.Lock();
        // reading, no let writer accss buf1
        g_eventBuf1.ResetEvent();
    }
    g_csReaderCount1.Unlock(); // 这个时候释放,那么另一个reader怎么读啊?