windows 네트웍 서버 프로그램에서 stl map은 스레드로 부터 안

dionysos의 이미지

공부할겸 iocp를 이용해서 게임서버를 제작하고 있습니다.

여기서 client list나 id list등등을 관리할때 c++ stl map을 주로

이용하고 있습니다. 이것들은 thread safe하나요

예를들어 유저리스트를 검색하고 있는데 또다른 클라이언트가 로그

인해서 사용중인 stl map에 엑세스가 된다든지.입니다.

그리고 만약 안전하지 않다면 크리티컬섹션이나 이벤트, 등 동기화

객체로 감싸줘야 하나요

익명 사용자의 이미지

The SGI implementation of STL is thread-safe only in the sense that simultaneous accesses to distinct containers are safe, and simultaneous read accesses to to shared containers are safe. If multiple threads access a single container, and at least one thread may potentially write, then the user is responsible for ensuring mutual exclusion between the threads during the container accesses.

source: http://www.sgi.com/tech/stl/thread_safety.html

dionysos의 이미지

많은 도움이 된거 같습니다.

노력은 배반하지 않는다.

prolinko의 이미지

비주얼 C++에 들어있는 STL은 딩컴에서 만든 걸로 알고 있습니다. SGI 버전을 쓸려면 STLPort를 설치해야 합니다.

느낌상 쓰레드 구현은 딩컴도 위에 나온 것과 비슷한 수준일 것 같긴 합니다.

chadr의 이미지

vc 2003 닷넷에 있는 stl을 멀티스레드에서 사용을 해봤는데 thread safe하지 않는것 같더군요.. 동기화 객체를 이용해서 보호한것과 안한경우를 비교하니까 보호하지 않는 경우 insert나 push_back등에서 포인터에 관한 에러가 발생했습니다..

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

purewell의 이미지

chadr wrote:
vc 2003 닷넷에 있는 stl을 멀티스레드에서 사용을 해봤는데 thread safe하지 않는것 같더군요.. 동기화 객체를 이용해서 보호한것과 안한경우를 비교하니까 보호하지 않는 경우 insert나 push_back등에서 포인터에 관한 에러가 발생했습니다..

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfthreadsafetyinstandardclibrary.asp

MFC가 아닌 일반적인 프로젝트는 MT 옵션들이 기본적으로 안 붙음.

_____________________________
언제나 맑고픈 샘이가...
http://purewell.biz

chadr의 이미지

purewell wrote:
chadr wrote:
vc 2003 닷넷에 있는 stl을 멀티스레드에서 사용을 해봤는데 thread safe하지 않는것 같더군요.. 동기화 객체를 이용해서 보호한것과 안한경우를 비교하니까 보호하지 않는 경우 insert나 push_back등에서 포인터에 관한 에러가 발생했습니다..

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfthreadsafetyinstandardclibrary.asp

MFC가 아닌 일반적인 프로젝트는 MT 옵션들이 기본적으로 안 붙음.

물론 MT옵션을 붙인 상태에서 사용했었습니다.. 그래도 문제가 발생하더라구요.. 흠...

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

익명 사용자의 이미지

prolinko wrote:
비주얼 C++에 들어있는 STL은 딩컴에서 만든 걸로 알고 있습니다. SGI 버전을 쓸려면 STLPort를 설치해야 합니다.

SGI와 STLPort는 다른 구현입니다.

ps. 거의 모든 STL 구현에서 thread safe는 읽기만 해당됩니다.

쌀밥의 이미지

chadr wrote:
vc 2003 닷넷에 있는 stl을 멀티스레드에서 사용을 해봤는데 thread safe하지 않는것 같더군요.. 동기화 객체를 이용해서 보호한것과 안한경우를 비교하니까 보호하지 않는 경우 insert나 push_back등에서 포인터에 관한 에러가 발생했습니다..

맨 처음 인용해주신 글의 내용을 보면
손님 wrote:
If multiple threads access a single container, and at least one thread may potentially write, then the user is responsible for ensuring mutual exclusion between the threads during the container accesses.

라고 되어있는데,
여러 쓰레드에서 한개의 map에 접근하고, 그중 한 쓰레드가 쓰기를 할 가능성이 있다면, 동기화 객체를 사용하는 것은 사용자의 책임이므로 알아서 해라.. 라고 되어있는데
말씀 하신것이 이 부분에 해당하는 것 같습니다.

이런 경우를 thread-safe하지 않다고 하지는 않는다고 생각됩니다.

thread-safe하지 않은 것은 localtime 이나 token (인가? 정확히 뭐였더라...; ) 같은 전역 변수를 사용하는 함수들 처럼 두개 이상이 동시에 동작할 수 없는 것들을 말하는 것 같습니다.
이런 경우 mutual exclusion 방법을 사용한다 하더라도 결과가 기대한것과 다르게 나오는 것이죠..

일하는 사람들의 희망 민주노동당 : http://www.kdlp.org
반공 교육의 성과로, 민주주의의 반대가 공산주의(또는 사회주의)라고 생각하는 사람이 많다.

deisys의 이미지

쌀밥 wrote:
chadr wrote:
vc 2003 닷넷에 있는 stl을 멀티스레드에서 사용을 해봤는데 thread safe하지 않는것 같더군요.. 동기화 객체를 이용해서 보호한것과 안한경우를 비교하니까 보호하지 않는 경우 insert나 push_back등에서 포인터에 관한 에러가 발생했습니다..

맨 처음 인용해주신 글의 내용을 보면
손님 wrote:
If multiple threads access a single container, and at least one thread may potentially write, then the user is responsible for ensuring mutual exclusion between the threads during the container accesses.

라고 되어있는데,
여러 쓰레드에서 한개의 map에 접근하고, 그중 한 쓰레드가 쓰기를 할 가능성이 있다면, 동기화 객체를 사용하는 것은 사용자의 책임이므로 알아서 해라.. 라고 되어있는데
말씀 하신것이 이 부분에 해당하는 것 같습니다.

이런 경우를 thread-safe하지 않다고 하지는 않는다고 생각됩니다.

thread-safe하지 않은 것은 localtime 이나 token (인가? 정확히 뭐였더라...; ) 같은 전역 변수를 사용하는 함수들 처럼 두개 이상이 동시에 동작할 수 없는 것들을 말하는 것 같습니다.
이런 경우 mutual exclusion 방법을 사용한다 하더라도 결과가 기대한것과 다르게 나오는 것이죠..

http://en.wikipedia.org/wiki/Thread-safety

'여러 쓰레드에서 한개의 map에 접근하고, 그중 한 쓰레드가 쓰기를 할 가능성이 있다면, 동기화 객체를 사용하는 것은 사용자의 책임이므로 알아서 해라' 라는건 '이건 Thread-safe하지 않으니 알아서 achieve thread-safety 하여라' 아닌가요?

앗, 그리고 전역변수를 사용하는 함수의 경우에 thread-safe한 코드를 만들 수 없는 경우가 있나요? 언뜻 떠오르는게 없는데 설명좀 부탁드립니다.

;-)

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.