두 쓰레드간에 메모리 할당 사이에도 동기화가 필요하나요?

freezm7의 이미지

두자리수가 넘는 쓰레드가 필요한 프로그램을 작성 중인데요,

쓰레드가 메모리 할당 함수 malloc 이나 new 연산자를 호출 시에
호출된 메모리 할당자 내부에서 동기화가 일어나나요?

동기화가 되지 않는다면, 힙에 대한 정보등이 상당히 위험해질 것 같은데요...

자세히 아시는 분 조언 부탁드려요.

(쓰레드 생성은 pthread_create 함수를 사용했습니다.)

최종호의 이미지

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 섹션 내용의 각주에서

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 함수를 정의해서 대체하면 된다.

정도 되겠네요.

echo의 이미지

위분 께서 님의 질문에 대한 답을 잘 설명해 주신 거 같습니다.
혹 그로인한 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

도움이 되셨길..

다들 자기 이야기 밖에 할 줄 모르는 세상에서,
메아리 같은 사람 하나 필요할 거 같아서...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.