C++의 new 연산자는 system interface가 아니기 때문에 Posix에 의해 정의되지 않습니다.
C++ 표준에서는 global allocation/deallocation function이 어떤 시스템 인터페이스를 사용하는지에 대해서 (당연히) 규정하고 있지 않습니다.
다만, 3.7.3.1 Allocation functions 섹션 내용의 각주에서
Quote:
The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially the
same. C + + differs from C in requiring a zero request to return a non-null pointer.
라고 되어있어서 대부분의 구현들이 malloc/calloc/realloc/free 를 사용하지 않을까 하는 추측을 남겨놓고 있습니다.
new 연산이 확실히 thread-safe 한 동작을 보장받고 싶다면
구현의 C++ 표준 라이브러리에 정의되어 있는 기본 버젼을 대체하는 함수를 정의한 후 해당 함수에서 malloc류를 호출하는 식으로 원하는 결과를 얻을 수 있을 것입니다.
(17.4.3.4)
Quote:
A C + + program may provide the definition for any of eight dynamic memory allocation function signatures
declared in header <new> (3.7.3, clause 18):
— operator new(size_t)
— operator new(size_t, const std::nothrow_t&)
— operator new[](size_t)
— operator new[](size_t, const std::nothrow_t&)
— operator delete(void*)
— operator delete(void*, const std::nothrow_t&)
— operator delete[](void*)
— operator delete[](void*, const std::nothrow_t&)
3 The program’s definitions are used instead of the default versions supplied by the implementation (18.4).
Such replacement occurs prior to program startup (3.2, 3.6).
간단히 얘기하면(세줄요약? -_-) ,
1. Posix 준수 시스템에서 malloc 은 thread-safe하다.
2. C++의 new도 아마 거의 thread-safe 하다고 심증이 간다. (C++ 라이브러리 구현에 따라 다르다)
3. C++에서 이를 보장받고 싶으면 alloc/dealloc 함수를 정의해서 대체하면 된다.
Posix를 준수하는 시스템이라면 malloc 은 thread-safe
Posix를 준수하는 시스템이라면 malloc 은 thread-safe 합니다.
Posix 표준(IEEE Std 1003.1-2004) System Interface Volume (XSH)의 Section 2.9.1, Thread-Safety 를 보면,
해당 섹션에서 나열한 함수들을 제외한 IEEE Std 1003.1-2001에 정의된 모든 함수는
thread-safe 해야 한다고 나와있습니다.
http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_09.html#tag_02_09_01
C++의 new 연산자는 system interface가 아니기 때문에 Posix에 의해 정의되지 않습니다.
C++ 표준에서는 global allocation/deallocation function이 어떤 시스템 인터페이스를 사용하는지에 대해서 (당연히) 규정하고 있지 않습니다.
다만, 3.7.3.1 Allocation functions 섹션 내용의 각주에서
라고 되어있어서 대부분의 구현들이 malloc/calloc/realloc/free 를 사용하지 않을까 하는 추측을 남겨놓고 있습니다.
new 연산이 확실히 thread-safe 한 동작을 보장받고 싶다면
구현의 C++ 표준 라이브러리에 정의되어 있는 기본 버젼을 대체하는 함수를 정의한 후 해당 함수에서 malloc류를 호출하는 식으로 원하는 결과를 얻을 수 있을 것입니다.
(17.4.3.4)
간단히 얘기하면(세줄요약? -_-) ,
1. Posix 준수 시스템에서 malloc 은 thread-safe하다.
2. C++의 new도 아마 거의 thread-safe 하다고 심증이 간다. (C++ 라이브러리 구현에 따라 다르다)
3. C++에서 이를 보장받고 싶으면 alloc/dealloc 함수를 정의해서 대체하면 된다.
정도 되겠네요.
performance 에 관련된 사항을 좀 더 알고 싶으시면...
위분 께서 님의 질문에 대한 답을 잘 설명해 주신 거 같습니다.
혹 그로인한 performance 에 관해 더 알고 싶으시다면,
다음 링크를 참고해 보세요.
Improve Performance with Thread Aware Memory Allocators
http://www.intel.com/cd/ids/developer/asmo-na/eng/dc/threading/methodology/43893.htm?page=1
The Hoard Multiprocessor Memory Allocator
http://www.hoard.org/
A Comparison of Memory Allocators in Multiprocessors
http://developers.sun.com/solaris/articles/multiproc/multiproc.html
Scaling of memory intensive multi-threaded applications to SMMP computers
http://www.codeproject.com/cpp/rtl_scaling.asp
도움이 되셨길..
다들 자기 이야기 밖에 할 줄 모르는 세상에서,
메아리 같은 사람 하나 필요할 거 같아서...
댓글 달기