测试

// test.cpp : ??????????????
//


#include "stdafx.h"
#include "Windows.h"


#define FIFO_LEN_MAX 1000
typedef struct _FIFO
{   
    HANDLE   handler_mutex;
HANDLE   handler_sem;
unsigned  int  fifo_len_max; 
unsigned  int  msg_count; 
    unsigned  int  write_point;      
    unsigned  int  read_point;       
    void *         msg_buf[FIFO_LEN_MAX];
}FIFO;


FIFO server_fifo;


int fifo_init(FIFO *p_fifo,unsigned int fifo_max_len)
{

if(fifo_max_len>FIFO_LEN_MAX)
{
return -1;
}

p_fifo->handler_mutex = CreateMutex(NULL,false,NULL); 
if(NULL==p_fifo->handler_mutex)
{
return -1;
}
p_fifo->handler_sem = CreateSemaphore(NULL,0,fifo_max_len,NULL);             
if(NULL==p_fifo->handler_sem)
{
return -1;
}




for(p_fifo->read_point=0;p_fifo->read_point<fifo_max_len;p_fifo->read_point++)
{
p_fifo->msg_buf[p_fifo->read_point] = NULL;
}




p_fifo->fifo_len_max = fifo_max_len;
p_fifo->msg_count   = 0;
p_fifo->write_point = 0;            
    p_fifo->read_point  = 0;


return 0;
}




int fifo_write(FIFO *p_fifo,void *p_msg,DWORD time_out)
{
DWORD result;

if( (p_fifo==((void*)0))||(p_msg==((void*)0)) )
{
return -1;
}


result = WaitForSingleObject(p_fifo->handler_mutex,time_out);
if((WAIT_TIMEOUT == result)||(WAIT_FAILED == result))//WAIT_ABANDONED
{
return -2;
}



if(p_fifo->msg_count < p_fifo->fifo_len_max)
    {
p_fifo->msg_count++;                               
        p_fifo->msg_buf[p_fifo->write_point] = p_msg;                    
        p_fifo->write_point++;                             
        if(p_fifo->write_point > (p_fifo->fifo_len_max-1)) 
{
        p_fifo->write_point = 0;
    }
}
else
{
ReleaseMutex(p_fifo->handler_mutex);
return -2;
}
ReleaseSemaphore(p_fifo->handler_sem,1,NULL);
ReleaseMutex(p_fifo->handler_mutex);
return 0;
}




void* fifo_read(FIFO *p_fifo,DWORD time_out,int *error)
{
DWORD result;
void *p_msg;

if( p_fifo==((void*)0))
{
*error = -1;
return NULL;
}

result = WaitForSingleObject(p_fifo->handler_sem,INFINITE);
if(WAIT_OBJECT_0 != result)
{
*error = -2;
return NULL;
}

result = WaitForSingleObject(p_fifo->handler_mutex,time_out);
if((WAIT_TIMEOUT == result)||(WAIT_FAILED == result))//WAIT_ABANDONED
{
ReleaseSemaphore(p_fifo->handler_sem,1,NULL);
*error = -3;
return NULL;
}

    if( p_fifo->msg_count != 0 )
    {  
      p_fifo->msg_count--;                                    
        p_msg = p_fifo->msg_buf[p_fifo->read_point];     
        p_fifo->read_point++;                                 
        if( p_fifo->read_point > (p_fifo->fifo_len_max-1) ) 
        {
p_fifo->read_point = 0;
        }
    }
else
{
*error = -4;
return NULL;
}

ReleaseMutex(p_fifo->handler_mutex);
*error = 0;
return p_msg;
}






DWORD WINAPI son_thread(void* lpParam)
{
int error;
unsigned int *p_msg;
printf("Son thread running....!\r\n");
while(1)

p_msg = (unsigned int *)fifo_read(&server_fifo,1000,&error);
if(NULL==p_msg)
{
printf("Son: fifo read error! \r\n");
}


printf("Son: Read fifo - %d \r\n",*p_msg);


//Sleep(3000);
}
return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
int result;
unsigned int *p_msg;
unsigned int count=0;

result = fifo_init(&server_fifo,500);
if(0!=result)
{
printf("Main: init fifo error! \r\n");
while(1);
}


CreateThread(NULL,0,son_thread,NULL,0,NULL);


while(1)
{
p_msg = (unsigned int *)HeapAlloc(GetProcessHeap(),0,sizeof(unsigned int));
*p_msg = count++;
result =  fifo_write(&server_fifo,p_msg,100);
if(0!=result)
{
printf("Main: write fifo error! \r\n");
continue;
}


printf("Main: write fifo:%d \r\n",count-1);


Sleep(1000);
}


return 0;
}








//HANDLE handle_Semaphore;
//HANDLE handle_Mutex;
//
//DWORD WINAPI son_thread(void* lpParam)
//{
// printf("Son thread running....!\r\n");
// while(1)
//
// WaitForSingleObject(handle_Mutex,INFINITE);
// printf("Son: Got Mutex! \r\n");
// Sleep(3000);
//
// ReleaseMutex(handle_Mutex);
// printf("Son: Release Sem! \r\n");
//
// }
// return 0;
//}
//
//int _tmain(int argc, _TCHAR* argv[])
//{
// DWORD  sem_result;
//
// //handle_Semaphore = CreateSemaphore(NULL,1,1000,NULL);
// //handle_Mutex= CreateMutex(NULL,false,"MY_MUTEX");
//
// handle_Mutex =  CreateMutex(NULL, false, "MY_MUTEX");
//
// CreateThread(NULL,0,son_thread,NULL,0,NULL);
//
// while(1)
// {
// //printf("Main: Wait for Sem! \r\n");
//
// WaitForSingleObject(handle_Mutex,INFINITE);
//
// printf("Main: 1 \r\n");
// Sleep(800);
// printf("Main: 2 \r\n");
// Sleep(800);
// printf("Main: 3 \r\n");
// Sleep(800);
// printf("Main: 4 \r\n");
// Sleep(800);
// printf("Main: 5 \r\n");
//
// ReleaseMutex(handle_Mutex);
//
// //printf("Main: Got Sem! \r\n");
// Sleep(1000);
// }
//
// return 0;
//}


//HANDLE handle_Semaphore;
//HANDLE handle_Mutex;
//DWORD WINAPI son_thread(void* lpParam)
//{
// printf("Son thread running....!\r\n");
// while(1)
//
// Sleep(3000);
//
// printf("Son: Release Sem! \r\n");
// if(FALSE == ReleaseSemaphore(handle_Semaphore,3,NULL))
// {
// printf("Son: Release Sem Error! \r\n");
// }
// }
// return 0;
//}
//int _tmain(int argc, _TCHAR* argv[])
//{
// DWORD  sem_result;
// handle_Semaphore = CreateSemaphore(NULL,1,1000,NULL);
// CreateThread(NULL,0,son_thread,NULL,0,NULL);
//
// while(1)
// {
// printf("Main: Wait for Sem! \r\n");
// sem_result = WaitForSingleObject(handle_Semaphore,INFINITE);
// if(WAIT_OBJECT_0!=sem_result)
// {
// continue;
// }
// printf("Main: Got Sem! \r\n");
// Sleep(10);
// }
// return 0;
//}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值