Windows C++多线程程序如何调适?

断点调适不适用了,应该怎么调适呢?我看有说log打印信息的,怎么实现呢?或者推荐一些文章吧。

对,设置断点,你会容易阻塞当前线程,从而让多线程的一些交互不能跟运行时一样
log输出就是在各个线程中打印一些日志信息,然后分析log文件来观察线程的运行状态
可以参考log4cplus
c++开源日志库log4cplus

1>e:\vcfile\vc_teaching\log4cplustest\log4cplustest\main.cpp(13) : error C2665: “log4cplus::Logger::getInstance”: 2 个重载中没有一个可以转换所有参数类型 1> d:\program files\microsoft visual studio 9.0\vc\include\log4cplus\logger.h(93): 可能是“log4cplus::Logger log4cplus::Logger::getInstance(const log4cplus::tstring &)” 1> 试图匹配参数列表“(const char [5])”时 1>e:\vcfile\vc_teaching\log4cplustest\log4cplustest\main.cpp(13) : error C2512: “log4cplus::Logger”: 没有合适的默认构造函数可用

VS 输出日志 用DebugOutputStringA/DebugOutputStringW(Unicode).

可以先用wsprintfA/wsprintfW(Unicode)格式化日志,在调用DebugOutputStringA/DebugOutputStringW输出。

这个是在http://bbs.csdn.net/topics/390341664这篇帖子里看到的,别人写的一个简单的LOG程序,我仔细研究了一下觉得也可以,但不太明白为什么作者说他自己的这个log尺有所短,寸有所长?各位专家觉得这样写log对原程序影响大吗?

 #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 ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
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;
#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;
    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);
            }
        } 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;
}

这种需要在全速运行下才能出现的BUG,一定要有日志才能定位。就算是自己定义一个log类也不是很困难吧,相信对于一个深入到多线程的人来说,弄个日志妥妥的。

在日志上标出时间和线程ID就可以了。

FILE *p = fopen("log.txt", "a+");
if (NULL != p)
{
fprintf(p, "write something you want");
fclose(p);
}

你可以使用VS的调试窗口输出来进行多线程调试:
outputdebugstring("")这个函数,你看看,会在调试时输出到vs的output窗口上
TRACE()这个是宏定义

可以采用log的方式来调试,多线程注意是否需要同步的问题