c语言多线程mysql,c语言 多线程 数据库

该博客探讨了如何使用C++在多个线程间通过固定大小的缓冲区传输数据。具体实现包括一个循环发送200字节数据的线程和一个接收并组合数据以生成240字节新缓冲区的线程。在处理过程中,程序会对数据进行十六进制转储,并使用日志记录关键操作。此外,还涉及到了临界区同步和日志文件的滚动管理。
摘要由CSDN通过智能技术生成

C/C++ code//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,

//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,

//在处理的时候每次从新buffer中取两个字节打印

#include

#include

#include

#include

#include

#include

//Log{

#define MAXLOGSIZE 10000000

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

#include

#include

#include

char logfilename1[]="MyLog1.log";

char logfilename2[]="MyLog2.log";

char logstr[16000];

char datestr[16];

char timestr[16];

char mss[4];

CRITICAL_SECTION cs_log;

FILE *flog;

void Lock(CRITICAL_SECTION *l) {

EnterCriticalSection(l);

}

void Unlock(CRITICAL_SECTION *l) {

LeaveCriticalSection(l);

}

void LogV(const char *pszFmt,va_list argp) {

struct tm *now;

struct timeb tb;

if (NULL==pszFmt||0==pszFmt[0]) return;

if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;

ftime(&tb);

now=localtime(&tb.time);

sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);

sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );

sprintf(mss,"%03d",tb.millitm);

printf("%s %s.%s %s",datestr,timestr,mss,logstr);

flog=fopen(logfilename1,"a");

if (NULL!=flog) {

fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);

if (ftell(flog)>MAXLOGSIZE) {

fclose(flog);

if (rename(logfilename1,logfilename2)) {

remove(logfilename2);

rename(logfilename1,logfilename2);

}

flog=fopen(logfilename1,"a");

if (NULL==flog) return;

}

fclose(flog);

}

}

void Log(const char *pszFmt,...) {

va_list argp;

Lock(&cs_log);

va_start(argp,pszFmt);

LogV(pszFmt,argp);

va_end(argp);

Unlock(&cs_log);

}

//Log}

#define ASIZE 200

#define BSIZE 240

#define CSIZE 2

char Abuf[ASIZE];

char Cbuf[CSIZE];

CRITICAL_SECTION cs_HEX ;

CRITICAL_SECTION cs_BBB ;

struct FIFO_BUFFER {

int head;

int tail;

int size;

char data[BSIZE];

} BBB;

int No_Loop=0;

void HexDump(int cn,char *buf,int len) {

int i,j,k;

char binstr[80];

Lock(&cs_HEX);

for (i=0;i

if (0==(i%16)) {

sprintf(binstr,"%03d %04x -",cn,i);

sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);

} else if (15==(i%16)) {

sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);

sprintf(binstr,"%s ",binstr);

for (j=i-15;j<=i;j++) {

sprintf(binstr,"%s%c",binstr,('!'

}

Log("%s\n",binstr);

} else {

sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);

}

}

if (0!=(i%16)) {

k=16-(i%16);

for (j=0;j

sprintf(binstr,"%s ",binstr);

}

sprintf(binstr,"%s ",binstr);

k=16-k;

for (j=i-k;j

sprintf(binstr,"%s%c",binstr,('!'

}

Log("%s\n",binstr);

}

Unlock(&cs_HEX);

}

int GetFromRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {

int lent,len1,len2;

lent=0;

Lock(cs);

if (fbuf->size>=len) {

lent=len;

if (fbuf->head+lent>BSIZE) {

len1=BSIZE-fbuf->head;

memcpy(buf ,fbuf->data+fbuf->head,len1);

len2=lent-len1;

memcpy(buf+len1,fbuf->data ,len2);

fbuf->head=len2;

} else {

memcpy(buf ,fbuf->data+fbuf->head,lent);

fbuf->head+=lent;

}

fbuf->size-=lent;

}

Unlock(cs);

return lent;

}

void thdB(void *pcn) {

char *recv_buf;

int recv_nbytes;

int cn;

int wc;

int pb;

cn=(int)pcn;

Log("%03d thdB thread begin...\n",cn);

while (1) {

Sleep(10);

recv_buf=(char *)Cbuf;

recv_nbytes=CSIZE;

wc=0;

while (1) {

pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);

if (pb) {

Log("%03d recv %d bytes\n",cn,pb);

HexDump(cn,recv_buf,pb);

Sleep(1);

} else {

Sleep(1000);

}

if (No_Loop) break;//

wc++;

if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);

}

if (No_Loop) break;//

}

}

int PutToRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {

int lent,len1,len2;

Lock(cs);

lent=len;

if (fbuf->size+lent>BSIZE) {

lent=BSIZE-fbuf->size;

}

if (fbuf->tail+lent>BSIZE) {

len1=BSIZE-fbuf->tail;

memcpy(fbuf->data+fbuf->tail,buf ,len1);

len2=lent-len1;

memcpy(fbuf->data ,buf+len1,len2);

fbuf->tail=len2;

} else {

memcpy(fbuf->data+fbuf->tail,buf ,lent);

fbuf->tail+=lent;

}

fbuf->size+=lent;

Unlock(cs);

return lent;

}

void thdA(void *pcn) {

char *send_buf;

int send_nbytes;

int cn;

int wc;

int a;

int pa;

cn=(int)pcn;

Log("%03d thdA thread begin...\n",cn);

a=0;

while (1) {

Sleep(100);

memset(Abuf,a,ASIZE);

a=(a+1)%256;

if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop

send_buf=(char *)Abuf;

send_nbytes=ASIZE;

Log("%03d sending %d bytes\n",cn,send_nbytes);

HexDump(cn,send_buf,send_nbytes);

wc=0;

while (1) {

pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);

Log("%03d sent %d bytes\n",cn,pa);

HexDump(cn,send_buf,pa);

send_buf+=pa;

send_nbytes-=pa;

if (send_nbytes<=0) break;//

Sleep(1000);

if (No_Loop) break;//

wc++;

if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);

}

if (No_Loop) break;//

}

}

int main() {

InitializeCriticalSection(&cs_log );

Log("Start===========================================================\n");

InitializeCriticalSection(&cs_HEX );

InitializeCriticalSection(&cs_BBB );

BBB.head=0;

BBB.tail=0;

BBB.size=0;

_beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);

_beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);

if (!access("No_Loop",0)) {

remove("No_Loop");

if (!access("No_Loop",0)) {

No_Loop=1;

}

}

while (1) {

Sleep(1000);

if (No_Loop) break;//

if (!access("No_Loop",0)) {

No_Loop=1;

}

}

Sleep(3000);

DeleteCriticalSection(&cs_BBB );

DeleteCriticalSection(&cs_HEX );

Log("End=============================================================\n");

DeleteCriticalSection(&cs_log );

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值