나는 여러 스레드 프로그램을 만드는 오전 여러 스레드가 전역 함수를동시 로그 파일 액세스/C++
writeLog(const char* pMsg);
를 호출해야하고 WRITELOG이 tihs 같은 구현 뭔가있을 것입니다 :
void writeLog(const char* pMsg)
{
CRITICAL_SECTION cs;
// initialize critical section
...
EnterCriticalSection(&cs);
// g_pLogFilePath is a global variable.
FILE *file;
if (0!=fopen_s(&file, g_pLogFilePath, "r+"))
return;
fprintf(file, pMsg);
fclose(file):
LeaveCriticalSection(&cs);
}
내 질문은 :
1) is it the best way to do concurrent logging? i.e., using critical section.
2) since I will write log in many places in the threads,
and since each log writing will involve open/close file,
does the io will impact the performance significantly?
감사합니다!
'cs'가 지역 변수 인 경우,'writeLog'를 입력 할 때마다 새로운 임계 영역을 생성하고 (반환 할 때 누출됩니다), 따라서 중요한 섹션은별로 없습니다. –
중요 섹션 개체는 전역이어야하며 [MSDN 참조] (http://msdn.microsoft.com/en-us/library/windows/desktop/ms686908%28v=vs.85%29.aspx)이어야합니다. –