.................bug

// fifo.cpp : 定义控制台应用程序的入口点。
//

#include 
"stdafx.h"

#include 
<windows.h>



#define BUF_MAX 1024
#define LIST_MAX  4

struct LIST
{
    
volatile int head;
    
volatile int tail;
    
volatile int empty;
    
volatile int full;
    
    size_t size[LIST_MAX];
    
char* buff;
};


struct LIST swreg;

HANDLE hWrite;
HANDLE hRead;


void wait_write()
{
    printf(
"%s\n",__FUNCTION__);
    ::WaitForSingleObject(hWrite,INFINITE);
}
void wait_read()
{
    printf(
"%s\n",__FUNCTION__);

    ::WaitForSingleObject(hRead,INFINITE);
}
void signel_write()
{
    printf(
"%s\n",__FUNCTION__);
    SetEvent(hWrite);
}
void signel_read()
{
    printf(
"%s\n",__FUNCTION__);
    SetEvent(hRead);

}



size_t writedata(
char* bufptr,size_t bufcnt)
{
    
int head;
    
int empty;
    size_t count;

    
if(bufcnt>BUF_MAX)
        
return -1;
        
        
    
if(swreg.full==1)
    {
        
while(1)
        {
            wait_write();
            
if(swreg.full==0)
                
break;
        }
    }    
    
    
    head
=swreg.head;
    
    count
=bufcnt;
    memcpy(swreg.buff
+head*BUF_MAX,bufptr,count);
    swreg.size[head]
=count;    
    
    
    head
=(head+1)%LIST_MAX;
    
if(head==swreg.tail)
        swreg.full
=1;

    
    empty
=swreg.empty;
    
//如果此时线程切换,程序死锁~ 在多CPU,采用共享内存和互相中断方式通讯中出现问题。还好,我只要外部加超时机制即可以避免问题。
    ::Sleep(1);
    swreg.empty
=0;
    swreg.head
=head;
    
    
if(empty)
        signel_read();



    
return bufcnt;
    
}



size_t readdata(
char* bufptr,size_t bufcnt)
{

    
int full;
    
int tail;

    size_t count;
    
if(swreg.empty==1)
    {
        
while(1)
        {
            wait_read();
            
if(swreg.empty==0)
                
break;
        }
    }

    tail
=swreg.tail;
    memcpy(bufptr,swreg.buff
+tail*BUF_MAX,swreg.size[tail]);
    count
=swreg.size[tail];



    tail
=(tail+1)%LIST_MAX;
    
if(swreg.head==tail)
        swreg.empty
=1;

    swreg.tail
=tail;
    full
=swreg.full;
    swreg.full
=0;  
    
if(full)
        signel_write();
        

    
return count;
}


DWORD WINAPI thread_write(LPVOID p)
{
    
int i=0;
    
char * sz[]=
    {
        
"sz0-1",
        
"sz1-12",
        
"sz2-123",
        
"sz3-12345",
        
"sz4-123456",
        
"sz5-1234567",
        
"sz6-12345678",
        
"sz7-123456789",
        
"sz8-1234567890",
        
"sz9-1234567890a",
    };
    
while(1)
    {
        
if(i==sizeof(sz)/sizeof(sz[0]))
            i
=0;
        writedata(sz[i],strlen(sz[i])
+1 );

        i
++;        
    }
    
return 0;
}

DWORD WINAPI thread_read(LPVOID p)
{
    
int i=0;
    
char sz[BUF_MAX];
    
while(1)
    {
        size_t s
=readdata(sz,BUF_MAX );
        printf(
"%d:%s,length(%d)\n",i++,sz,s);
        
    }

    
return 0;
}



int _tmain(int argc, _TCHAR* argv[])
{

    
char * buf=new char[LIST_MAX*BUF_MAX];


    hWrite
=CreateEvent(0,0,0,0);
    hRead
=CreateEvent(0,0,0,0);




    
if(buf)
    {
        memset(
&swreg,0,sizeof(swreg) );
        swreg.buff
=buf;
        swreg.empty
=1;


        DWORD ThreadId;

        CreateThread(NULL,NULL,thread_write,NULL,NULL,
&ThreadId);
        CreateThread(NULL,NULL,thread_read,NULL,NULL,
&ThreadId);



        
    }


    
char c=getchar();

    
while(c!=EOF)
    {
        c
=getchar();
    }

    delete buf;

    CloseHandle(hWrite);
    CloseHandle(hRead);

    
    
return 0;
}
原文地址:https://www.cnblogs.com/iwasmu/p/1971245.html