关于线程同步的问题
#include "stdafx.h"#include <windows.h>#include <iostream>#include <process.h>using namespace std;const int MAX_TIMES = 100;int g_nIndex = 0;DWORD g_dwTimes[MAX_TIMES];CRITICAL_SECTION g_cs;unsigned WINAPI ThreadProc1(LPVOID lparam){ cout << "This child thread 1" << endl; while(g_nIndex < MAX_TIMES) { EnterCriticalSection(&g_cs); g_dwTimes[g_nIndex] = g_nIndex; g_nIndex++; LeaveCriticalSection(&g_cs); } return 0;}unsigned WINAPI ThreadProc2(LPVOID lparam){ cout << "This child thread 2" << endl; int account = 0; while(g_nIndex > 0) { EnterCriticalSection(&g_cs); cout << g_dwTimes[g_nIndex-1] << " "; g_nIndex--; account++; if(account%10 == 0) cout << endl; LeaveCriticalSection(&g_cs); } return 0;}int main(int argc, char* argv[]){ InitializeCriticalSection(&g_cs); //create a child thread unsigned dwThreadId; unsigned vir; HANDLE HandleThread = (HANDLE)_beginthreadex(NULL,0,&ThreadProc1,&vir,0,&dwThreadId); //WaitForSingleObject(HandleThread,INFINITE); CloseHandle(HandleThread); unsigned dwThreadId2; unsigned vir2; HANDLE HandleThread2 = (HANDLE)_beginthreadex(NULL,0,&ThreadProc2,&vir2,0,&dwThreadId2); WaitForSingleObject(HandleThread2,INFINITE); CloseHandle(HandleThread2); DeleteCriticalSection(&g_cs); cout << "Hello World!" << endl; return 0;}
//结果1:ThiTsh icsh iclhdi ltdh rteharde a1d 299 98 97 96 95 94 93 92 91 9089 88 87 86 85 84 83 82 81 8079 78 77 76 75 74 73 72 71 7069 68 67 66 65 64 63 62 61 6059 58 57 56 55 54 53 52 51 5049 48 47 46 45 44 43 42 41 4039 38 37 36 35 34 33 32 31 3029 28 27 26 25 24 23 22 21 2019 18 17 16 15 14 13 12 11 109 8 7 6 5 4 3 2 1 0Hello World!Press any key to continue//结果2:ThTihsi sc hcihlildd t hthrreade 2aHdell o W1orld!Press any key to continue
#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 100000000#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 WIN32void Lock(CRITICAL_SECTION *l) { EnterCriticalSection(l);}void Unlock(CRITICAL_SECTION *l) { LeaveCriticalSection(l);}#elsevoid Lock(CRITICAL_SECTION *l) { pthread_mutex_lock(l);}void Unlock(CRITICAL_SECTION *l) { pthread_mutex_unlock(l);}#endifvoid 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}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;}
[解决办法]
cout没有同步