sendto,write 함수는 thread-safe 한가요?

byulparan의 이미지

같은 시간에, 같은 파일디스크립터를 향해, 복수개의 쓰레드에서 쓰기 작업을 한다면
결과가 어떻게 될까요?

구글링을 해보니, 절대적으로 안전하다. 라는 의견도 있고, 안전하지 않다고 이야기 하는
사람도 있어서 혼란스럽네요.

그냥 속편히 동기화된 큐같은 걸 이용해서, 보내면 되겠지만 만약 안전하다면, 구지
쓰레드를 하나 더 만드는게 낭비라는 생각이 들어서요.

가령 A 쓰레드에서 "AAAA" 를, B 쓰레드에서 "BBBB" 를 같은 시간에 같은 소켓을
향해서 쓸때, 받는쪽에서 "AABABBAB" 식으로 데이터가 망가질 수도 있는지 궁금합니다.

테스트를 여러번 돌려봐도 위와 같은 경우는 없었지만, 멀티쓰레드상에서 벌어지는 일이라
나온 결과를 신뢰 할 수 가 없네요.ㅎㅎ

쓸려고 하는건 udp 이긴 한데 tcp/udp 양 쪽 모두 궁금하네요.
작업환경은 linux 와 os x 입니다. 감사합니다.

익명 사용자의 이미지

thread-safe와 atomic을 혼동하신것 같네요.

UDP 통신에 사용되는 sendto 의 경우는 atomic 입니다.
그러나 write는 atomic이 아니지만 thread-safe 하지요.

즉 말씀하신 파일디스크립터에 멀티 쓰레드상에서 write할 때는
섞여서 씌여질 수 있습니다.

gilgil의 이미지

제가 예전에 테스트를 해 본 적이 있는데
send 함수(tcp data를 peer에게 보내는) 한번 콜을 하는 그 자체는 atomic한 것으로 알고 있습니다.

A thread에서 send("aaa...");
B thread에서 send("bbb...");
했을 경우 "aaa..."와 "bbb..."가 섞이지는 않구요,

A thread에서 send("aaa...");send("aaa...");send("aaa...");
B thread에서 send("bbb...");send("bbb...");send("bbb...");
에서는 섞이는 것을 확인하였습니다.

winner의 이미지

두 함수는 stateless, 즉 reentrant 합니다.

댓글 달기

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