首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于C语言中多线程应用中的一个BUG

2013-03-25 
关于C语言中多线程使用中的一个BUG//实现类似劲舞团中按上下左右四个键的程序,尚未作出准确与否的判断#inc

关于C语言中多线程使用中的一个BUG


//实现类似劲舞团中按上下左右四个键的程序,尚未作出准确与否的判断

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "process.h"
#include "windows.h"
#include<conio.h>
#define LEFT  0x4B
#define RIGHT 0x4D
#define UP    0x48
#define DOWN  0x50



DWORD WINAPI ThreadProc_Intinit( LPVOID lpParam )

int Rand = -1;
int i = 10;
fflush(stdin);
srand((unsigned int)time(NULL));

while(1)
{
while(i--)
{
while(1)
{
Rand = rand()%5;
if(0 != Rand) //随即产生的数放进数组中
break;
}
if(1 == Rand)
{
//Step[Index] = '1';
printf("%c\t",24);
}
else
if(2 == Rand)
{
//Step[Index] = '2';
printf("%c\t",25);
}
else
if(3 == Rand)
{
//Step[Index] = '3';
printf("%c\t",26);
}
else
if(4 == Rand)
{
//Step[Index] = '4';
printf("%c\t",27);
}
else
{
printf("ERROR!!!\n");
break;
}
}
printf("\n");
//printf("hello,this thread 1 ...\n");    //延时  
i = 10;
Sleep(1005);
}
//AfxEndThread();

//ExitThread(GetExitCodeThread(0,0));

}

DWORD WINAPI ThreadProc_Input( LPVOID lpParam ) 
{
int temp;

while(1)
{
if(_kbhit())                               
{

temp=_getch();   
temp=_getch();

switch(temp)       
{
case LEFT:            
printf("%c\t",27);break;  
case RIGHT:            
printf("%c\t",26);break;              
case UP:            
printf("%c\t",24);break;                   
case DOWN:            
printf("%c\t",25);break;               
default:
printf("ERROR!!!\n");
temp = _getch();

}   
}
}
}

DWORD WINAPI ThreadProc_Input_End( LPVOID lpParam ) 
{
//创建线程1       
CreateThread(             
NULL,               // default security attributes
0,                  // use default stack size              
ThreadProc_Input, // thread function             


NULL,               // argument to thread function             
0,                  // use default creation flags             
NULL);              // returns the thread identifier     
while(1)
{
Sleep(1000);
ExitThread(GetExitCodeThread(0,0));
}


///////////////////////////////////////////////////////////////////////////////////////
int main()
{

//创建线程1       
CreateThread(             
NULL,               // default security attributes
0,                  // use default stack size              
ThreadProc_Intinit, // thread function             
NULL,               // argument to thread function             
0,                  // use default creation flags             
NULL);              // returns the thread identifier     
//创建线程2        
CreateThread(             
NULL,              // default security attributes            
0,                 // use default stack size              
ThreadProc_Input_End,  // thread function             
NULL,             // argument to thread function             
0,                 // use default creation flags             
NULL);           // returns the thread identifier  
while(1)
{

Sleep(1000);
printf("\n新的开始:\n");
}

}


因为程序的需要,不得不用多线程实现,但是这段代码中的一个BUG一直找不出来,求明白的大侠指点!
BUG描述:程序运行的时候,如果一直不停的按上下左右四个按键的话,会影响到随即输出方向的个数,本来是输出10个上下左右四个键,如果不停的按的话,有可能会输出来11个,不明白为什么,是不是要做线程的互斥,应该怎么做呢,求解
运行平台:VS平台 c 多线程
------解决方案--------------------


不明白楼主的意思,我运行了下程序也没发现问题

BUG描述:程序运行的时候,如果一直不停的按上下左右四个按键的话,会影响到随即输出方向的个数,本来是输出10个上下左右四个键,如果不停的按的话,有可能会输出来11个,

这句话是什么意思?“本来输出10个”,什么是本来输出10个?楼主可以把ThreadProc_Intinit 这个线程输出的东西写到文件里面去,不要与其它线程输出的混淆了,看看是不是准确的
[解决办法]
特供多线程程序调试使用:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt
[解决办法]
0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    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);
            }
        } else {
            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}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}

热点排行