#include<stdio.h> #include<string.h> #include<stdlib.h> #include "Semaphore.h" #include "Mutex.h" class CCircleBuf { public: CCircleBuf() { m_state = false; } ~CCircleBuf() { } public: int Initial(int size) { if (m_state == true) { printf("it's already initial!\n"); return -1; } if(size & (size -1)) { printf("buffer size must be 2 Power\n"); return -1; } m_size = size; m_in = m_out = 0; m_buf = new unsigned char[size]; memset(m_buf, 0 , sizeof(size)); m_mutex.Create(); m_state = true; return 0; } int Destroy() { return 0; } unsigned short fifo_read(char *buf,unsigned short len) { m_mutex.lock(); unsigned short node_len = 0; if (0 == GetUsedlen()) { printf("********** there is no data to read!\r\n"); m_mutex.unlock(); } //读取数据长度 if (0 == Read((char*)&node_len, sizeof(node_len))) { printf("********** get node_len faild!\r\n"); m_mutex.unlock(); return 0; } //读取数据内容 if (len < node_len) { printf("********** buf size is small!\r\n"); m_mutex.unlock(); return 0; } if (0 == Read( buf, node_len)) { printf("********** get node_content faild!\r\n"); m_mutex.unlock(); return 0; } //优化,如果数据读取完毕则重置m_in,m_out if (0 == GetUsedlen()) { m_in = m_out = 0; } m_mutex.unlock(); return len; } unsigned short fifo_write(char *buf,unsigned short len) { m_mutex.lock(); if (GetUsedlen() + sizeof(len) + len > m_size) { printf("********** the room of buf is full!\r\n"); m_mutex.unlock(); return 0; } //写入文件长度 Write((char*)&len,sizeof(len)); //写入数据内容 Write(buf,len); m_mutex.unlock(); return len; } protected: unsigned short Read(char *buf,unsigned short len) { //获取使用空间大小 unsigned short used_size = GetUsedlen(); len = min(len,used_size); unsigned short lelf = min(len,m_size - m_out &(m_size -1)); memcpy(buf,m_buf + (m_out & (m_size -1)),lelf); memcpy(buf + lelf ,m_buf, len - lelf); m_out += len; return len; } unsigned short Write(char *buf,unsigned short len) { //获取使用空间大小 unsigned short used_size = GetUsedlen(); len = min(len , m_size - used_size); unsigned short lelf = min(len, m_size - (m_in &(m_size - 1))); memcpy(m_buf + (m_in &(m_size - 1)), buf, lelf); memcpy(m_buf, buf + lelf, len -lelf); m_in += len; return len; } unsigned short GetUsedlen() { return m_in - m_out; } private: bool m_state; unsigned char *m_buf; unsigned short m_size; unsigned short m_in; unsigned short m_out; Tools::Mutex m_mutex; }; DWORD WINAPI ThreadRead(LPVOID lparam) { char temp[1024] = {'\0'}; char buf[128] = {'\0'}; while(1) { memset(temp, 0 , sizeof(temp)); memset(buf, 0 , sizeof(buf)); if (gCircleBuf.fifo_read(buf,sizeof(buf)) > 0 ) { sprintf(temp,"read data:%s\r\n",buf); OutputDebugString(temp); } Sleep(1*1000); } return 0; } DWORD WINAPI ThreadWrite(LPVOID lparam) { srand(time(NULL)); while (1) { char buf[128] = {'\0'}; sprintf(buf,"(%d)",rand()); gCircleBuf.fifo_write(buf,strlen(buf)); Sleep(1*1000); } return 0; } int main() { gCircleBuf.Initial(1024); char *temp = new char[128]; unsigned short tt = 65535*2; unsigned short t1 = 65527; unsigned short t2 = 915; unsigned short t3 = 11; unsigned short t = min(t3,(t2-t1)); unsigned short offset = t2 - t1; HANDLE hTread1 = ::CreateThread(NULL, 0 , ThreadRead, NULL, 0 , NULL); HANDLE hTread2 = ::CreateThread(NULL, 0 , ThreadWrite, NULL, 0 , NULL); CloseHandle(hTread1); CloseHandle(hTread2); system("Pause"); return 0; }
#pragma once #include <windows.h> namespace Tools { class Semaphore { public: HANDLE m_handle; bool m_state; public: Semaphore():m_state(false),m_handle(NULL){} ~Semaphore(){ Destroy(); } int Create(unsigned short nInitialCount = 0, unsigned short nMaximumCount = 0xffff) { if (!m_state) { m_state = true; if (nInitialCount > nMaximumCount) { printf("param error!\r\n"); return -1; } m_handle = CreateSemaphore(NULL, nInitialCount, nMaximumCount, NULL ); if (NULL == m_handle) { printf("[%s %d] GetLastError:%d\r\n",__FUNCTION__,__LINE__,GetLastError()); return -1; } return 0; } return -1; } int Destroy() { if (m_state) { CloseHandle(m_handle); m_handle = NULL; m_state = false; return 0; } return -1; } int Signal() { if (m_state) { if (NULL == ReleaseSemaphore(m_handle,1,NULL)) { printf("[%s %d] GetLastError:%d\r\n",__FUNCTION__,__LINE__,GetLastError()); return -1; } return 0; } return -1; } int Wait() { if (m_state) { return Wait(INFINITE); } return -1; } int Wait(DWORD dwTime) { if (m_state) { int ret = WaitForSingleObject(m_handle,dwTime); if (WAIT_TIMEOUT == ret) { return -1; } else if (WAIT_OBJECT_0 == ret) { return 0; } else { printf("[%s %d] GetLastError:%d,ret:%d\r\n",__FUNCTION__,__LINE__,GetLastError(),ret); } } return -1; } }; }
#pragma once #include <windows.h> namespace Tools { class Mutex { public: HANDLE m_handle; bool m_state; public: Mutex():m_state(false),m_handle(NULL){} ~Mutex(){ Destroy(); } int Create() { if (!m_state) { m_handle = CreateMutex(NULL, FALSE, NULL); if (NULL == m_handle) { return -1; } } m_state = true; return 0; } int Destroy() { if (m_state) { CloseHandle(m_handle); m_handle = NULL; m_state = false; return 0; } return -1; } int unlock() { if (m_state) { if (NULL == ReleaseMutex(m_handle)) { return -1; } return 0; } return -1; } int lock() { if (m_state) { int ret = WaitForSingleObject(m_handle,INFINITE); if (WAIT_TIMEOUT == ret) { return -1; } else if (WAIT_OBJECT_0 == ret) { return 0; } } return -1; } int lock(DWORD dwTime) { if (m_state) { int ret = WaitForSingleObject(m_handle,dwTime); if (WAIT_TIMEOUT == ret) { return -1; } else if (WAIT_OBJECT_0 == ret) { return 0; } } return -1; } }; }