C 为什么多个线程得到的是相同的;工业贸易署;

C 为什么多个线程得到的是相同的;工业贸易署;,c,multithreading,pthreads,C,Multithreading,Pthreads,我的教授提供了上面的示例代码(没有复制以保留他的IP),我对输出感到困惑 有两个函数被用作启动例程,T1和T2,还有两个单独的用于启动新线程的循环。为每个线程分配了一个tid,该tid应与创建线程时的t值相匹配,但在其示例输出的末尾,有多个相同函数的线程具有相同的tid,即有两个T1线程具有tid 1。为什么会这样?如果有4个T1线程,它不应该生成TID 0-3吗?您的教授设计的这些线程不正确,t会更改其在主线程中的值,并被其他线程访问,而不使用互斥 下面是clang的TSan说的话: ====


我的教授提供了上面的示例代码(没有复制以保留他的IP),我对输出感到困惑


有两个函数被用作启动例程,T1和T2,还有两个单独的用于启动新线程的循环。为每个线程分配了一个tid,该tid应与创建线程时的t值相匹配,但在其示例输出的末尾,有多个相同函数的线程具有相同的tid,即有两个T1线程具有tid 1。为什么会这样?如果有4个T1线程,它不应该生成TID 0-3吗?

您的教授设计的这些线程不正确,
t
会更改其在主线程中的值,并被其他线程访问,而不使用互斥

下面是
clang
的TSan说的话:

==================
WARNING: ThreadSanitizer: data race (pid=5810)
  Read of size 4 at 0x7fff193c03e4 by thread T1:
    #0 T1(void*) /home/brian/src/so/threading/ex.cpp:16 (exe+0x0000000a0497)

  Previous write of size 4 at 0x7fff193c03e4 by main thread:
    #0 main /home/brian/src/so/threading/ex.cpp:61 (exe+0x0000000a0881)

  Location is stack of main thread.

  Thread T1 (tid=5812, running) created by main thread at:
    #0 pthread_create ??:0 (exe+0x000000045a8b)
    #1 main /home/brian/src/so/threading/ex.cpp:62 (exe+0x0000000a085a)

SUMMARY: ThreadSanitizer: data race /home/brian/src/so/threading/ex.cpp:16 T1(void*)
==================
。。。接着是

T1 [0] count = 12
==================
WARNING: ThreadSanitizer: data race (pid=5810)
  Write of size 1 at 0x7ff2f8aa2c80 by main thread:
    #0 main /home/brian/src/so/threading/ex.cpp:70 (exe+0x0000000a0964)

  Previous read of size 1 at 0x7ff2f8aa2c80 by thread T1:
    #0 T1(void*) /home/brian/src/so/threading/ex.cpp:18 (exe+0x0000000a04de)

  As if synchronized via sleep:
    #0 sleep ??:0 (exe+0x00000003f7bd)
    #1 main /home/brian/src/so/threading/ex.cpp:69 (exe+0x0000000a0952)

  Thread T1 (tid=5812, running) created by main thread at:
    #0 pthread_create ??:0 (exe+0x000000045a8b)
    #1 main /home/brian/src/so/threading/ex.cpp:62 (exe+0x0000000a085a)

SUMMARY: ThreadSanitizer: data race /home/brian/src/so/threading/ex.cpp:70 main
==================
T1 thread 1 done.

您的教授错误地设计了这些函数,
t
在主线程中更改了它的值,其他线程在不使用互斥的情况下访问它

下面是
clang
的TSan说的话:

==================
WARNING: ThreadSanitizer: data race (pid=5810)
  Read of size 4 at 0x7fff193c03e4 by thread T1:
    #0 T1(void*) /home/brian/src/so/threading/ex.cpp:16 (exe+0x0000000a0497)

  Previous write of size 4 at 0x7fff193c03e4 by main thread:
    #0 main /home/brian/src/so/threading/ex.cpp:61 (exe+0x0000000a0881)

  Location is stack of main thread.

  Thread T1 (tid=5812, running) created by main thread at:
    #0 pthread_create ??:0 (exe+0x000000045a8b)
    #1 main /home/brian/src/so/threading/ex.cpp:62 (exe+0x0000000a085a)

SUMMARY: ThreadSanitizer: data race /home/brian/src/so/threading/ex.cpp:16 T1(void*)
==================
。。。接着是

T1 [0] count = 12
==================
WARNING: ThreadSanitizer: data race (pid=5810)
  Write of size 1 at 0x7ff2f8aa2c80 by main thread:
    #0 main /home/brian/src/so/threading/ex.cpp:70 (exe+0x0000000a0964)

  Previous read of size 1 at 0x7ff2f8aa2c80 by thread T1:
    #0 T1(void*) /home/brian/src/so/threading/ex.cpp:18 (exe+0x0000000a04de)

  As if synchronized via sleep:
    #0 sleep ??:0 (exe+0x00000003f7bd)
    #1 main /home/brian/src/so/threading/ex.cpp:69 (exe+0x0000000a0952)

  Thread T1 (tid=5812, running) created by main thread at:
    #0 pthread_create ??:0 (exe+0x000000045a8b)
    #1 main /home/brian/src/so/threading/ex.cpp:62 (exe+0x0000000a085a)

SUMMARY: ThreadSanitizer: data race /home/brian/src/so/threading/ex.cpp:70 main
==================
T1 thread 1 done.

因此,当main已经增加它的值时,一个新线程正在分配它的tid值?是的,行为是不确定的。例如,所有线程都可以屈服,并且都以相同的值结束。明白了。也谢谢你指出TSan,这看起来非常有用。所以,任何时候在pthread_create()中传递的参数可能会在不同的线程之间更改,我都希望确保它位于互斥锁中。。。这很快就会变得复杂。一方面,是的,它确实复杂。我个人认为这个概念是数百万个bug的核心。OTHH有很多简单的构造,用于锁定/同步,比如C++、java、python。忘记我所说的C++、java、Python——你的目标是了解这些东西是如何工作的,而不仅仅是利用同步机制本身。不需要让讲师感到尴尬,但可以借此机会了解出错的容易程度。因此,当main已经增加它的值时,一个新线程正在分配它的tid值?是的,行为是不确定的。例如,所有线程都可以屈服,并且都以相同的值结束。明白了。也谢谢你指出TSan,这看起来非常有用。所以,任何时候在pthread_create()中传递的参数可能会在不同的线程之间更改,我都希望确保它位于互斥锁中。。。这很快就会变得复杂。一方面,是的,它确实复杂。我个人认为这个概念是数百万个bug的核心。OTHH有很多简单的构造,用于锁定/同步,比如C++、java、python。忘记我所说的C++、java、Python——你的目标是了解这些东西是如何工作的,而不仅仅是利用同步机制本身。不必让讲师难堪,但可以借此机会了解出错的容易程度。“不复制以保护其知识产权”——至少在美国,版权对这样的使用特别自由。请随意复制类似的其他来源,并引用它们最初的出版地点。“不复制以保护其知识产权”——至少在美国,版权对这样的使用特别自由。请随意复制像这样的其他来源,并引用它们最初发表的地方。