반응형
1. CreateThread :
   Windows API함수이며 몇몇
   몇몇 crt 함수 (malloc(), fopen(), _open(), strtok(), ctime(), or localtime() 등등)가 만들어 놓은 static data 및 static buffer를 clean up하지 못함
   ExitThread에서 Thread에 생성된 static buffer를 clean up하지 못하므로 memory leak이 발생함(70~80byte)
   그런데 실제로 thread-safe 여부에 대해서는 언급은 없음
    thread 마다 고유의 static buffer를 생성하기 때문에 서로 침범을 하거나 critical section문제는 없을 것 같음
    CreateThread은 사용하지 않기를 절대적으로 권고하지만, 사용했다고 해서 thread관련 문제가 발생하지는 않음
    (MSDN에 MS VC++ 1.0부터 적용되는 내용이라는 걸 보아 문제는 안된다고 거의 말할 수 있음)

2. _beginthreadex : 
   _beginthread의 버그를 수정
   CRT함수들에 대해 문제없이 사용가능함 
   내부적으로는 CreateThread를 이용하여 구현되어 있음

3. AfxBeginThread
   MFC의 CWinThread클래스의 CreateThread를 호출
   CWinThread::CreateThread는 내부적으로 _binthreadex로 구현되어있으므로 CRT함수들에 대해 문제없이 사용가능


참고자료
Creating Threads
CreateThread Function
_beginthread, _beginthreadex 
AfxBeginThread
C Run-Time Libraries (CRT)

Description of using C Run-Time (CRT) functions 

and CreateThread () 

http://support.microsoft.com/default.aspx/kb/104641/en-us



반응형
반응형

fopen함수가 multithread가 안전한가라는 주제로 웹에서 나온 자료
http://blog.naver.com/process3/20093839045

open 과 fopen 계열 함수들의 차이점과 적용시점


// crt_fopen.c
// compile with: /W3
// This program opens two files. It uses
// fclose to close the first file and
// _fcloseall to close all remaining files.

#include <stdio.h>

FILE *stream, *stream2;

int main( void )
{
   int numclosed;

   // Open for read (will fail if file "crt_fopen.c" does not exist)
   if( (stream  = fopen( "crt_fopen.c", "r" )) == NULL ) // C4996
   // Note: fopen is deprecated; consider using fopen_s instead
      printf( "The file 'crt_fopen.c' was not opened\n" );
   else
      printf( "The file 'crt_fopen.c' was opened\n" );

   // Open for write 
   if( (stream2 = fopen( "data2", "w+" )) == NULL ) // C4996
      printf( "The file 'data2' was not opened\n" );
   else
      printf( "The file 'data2' was opened\n" );

   // Close stream if it is not NULL 
   if( stream)
   {
      if ( fclose( stream ) )
      {
         printf( "The file 'crt_fopen.c' was not closed\n" );
      }
   }

   // All other files are closed: 
   numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}

Example

// crt_fopen_s.c
// This program opens two files. It uses
// fclose to close the first file and
// _fcloseall to close all remaining files.
 

#include <stdio.h>

FILE *stream, *stream2;

int main( void )
{
   errno_t err;

   // Open for read (will fail if file "crt_fopen_s.c" does not exist)
   err  = fopen_s( &stream, "crt_fopen_s.c", "r" );
   if( err == 0 )
   {
      printf( "The file 'crt_fopen_s.c' was opened\n" );
   }
   else
   {
      printf( "The file 'crt_fopen_s.c' was not opened\n" );
   }

   // Open for write 
   err = fopen_s( &stream2, "data2", "w+" );
   if( err == 0 )
   {
      printf( "The file 'data2' was opened\n" );
   }
   else
   {
      printf( "The file 'data2' was not opened\n" );
   }

   // Close stream if it is not NULL 
   if( stream )
   {
      err = fclose( stream );
      if ( err == 0 )
      {
         printf( "The file 'crt_fopen_s.c' was closed\n" );
      }
      else
      {
         printf( "The file 'crt_fopen_s.c' was not closed\n" );
      }
   }

   // All other files are closed:
   int numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}
Example

// crt_fsopen.c

#include <stdio.h>
#include <stdlib.h>
#include <share.h>

int main( void )
{
   FILE *stream;

   // Open output file for writing. Using _fsopen allows us to
   // ensure that no one else writes to the file while we are
   // writing to it.
    //
   if( (stream = _fsopen( "outfile", "wt", _SH_DENYWR )) != NULL )
   {
      fprintf( stream, "No one else in the network can write "
                       "to this file until we are done.\n" );
      fclose( stream );
   }
   // Now others can write to the file while we read it.
   system( "type outfile" );
}
반응형
반응형
반응형
반응형
Bit Fields에 대해서 궁금한 게 좀 있었는데
문서에 정확한 설명이 나와있네요..

아래의 force alignment 부분은 좀 유의해야겠습니다.
unsigned : 0; // Force alignment to next boundary. 


http://msdn.microsoft.com/en-us/library/ms858673.aspx
C/C++ Language Reference
C++ Bit Fields

Classes and structures can contain members that occupy less storage than an integral type. These members are specified as bit fields. The syntax for bit-field member-declarator specification follows:

declaratoropt  : constant-expression

The declarator is the name by which the member is accessed in the program. It must be an integral type (including enumerated types). The constant-expression specifies the number of bits the member occupies in the structure. Anonymous bit fields — that is, bit-field members with no identifier — can be used for padding.

Note   An unnamed bit field of width 0 forces alignment of the next bit field to the next type boundary, where type is the type of the member.

The following example declares a structure that contains bit fields:

struct Date
{
    unsigned nWeekDay  : 3;    // 0..7   (3 bits)
    unsigned nMonthDay : 6;    // 0..31  (6 bits)
    unsigned nMonth    : 5;    // 0..12  (5 bits)
    unsigned nYear     : 8;    // 0..100 (8 bits)
};

The conceptual memory layout of an object of type Date is shown in Figure 8.2.

Figure 8.2   Memory Layout of Date Object

Note that nYear is 8 bits long and would overflow the word boundary of the declared type, unsigned int. Therefore, it is begun at the beginning of a new unsigned int. It is not necessary that all bit fields fit in one object of the underlying type; new units of storage are allocated, according to the number of bits requested in the declaration.

Microsoft Specific

The ordering of data declared as bit fields is from low to high bit, as shown in Figure 8.2.

END Microsoft Specific

If the declaration of a structure includes an unnamed field of length 0, as shown in the following example,

struct Date
{
    unsigned nWeekDay  : 3;    // 0..7   (3 bits)
    unsigned nMonthDay : 6;    // 0..31  (6 bits)
    unsigned           : 0;    // Force alignment to next boundary.
    unsigned nMonth    : 5;    // 0..12  (5 bits)
    unsigned nYear     : 8;    // 0..100 (8 bits)
};

the memory layout is as shown in Figure 8.3.

Figure 8.3   Layout of Date Object with Zero-Length Bit Field

The underlying type of a bit field must be an integral type, as described in Fundamental Types in Chapter 2.

반응형
반응형
255자가 넘는 path를 지원을 위해서는 아래와 같이 사용해야 함
\\?\C:\folder or filename
\\?\UNC\eric-pc\shared

namespace : 
\\?\는 Win32 File namespace
\\.\는 Win32 Device namespace
\\.\COM1 or \\.\PhysicalDiskX

if(strFilePath.Left(2).CompareNoCase("\\\\") == 0) { strFilePath = "\\\\?\\UNC\\" + strFilePath.Mid(2); } else { strFilePath = "\\\\?\\" + strFilePath; }

반응형

+ Recent posts