☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

多线程调用标准库函数造成数据竞争

C++-Undefined-Behavior-86
目录 › next › previous

某些库函数与共享数据相关,但并未提供安全的同步机制,在多线程环境中使用会导致未定义的行为。

示例:

void foo() {
    while (true) {
        auto r = rand();   // Undefined behavior
        ....
    }
}

int main() {
    thread t0(foo), t1(foo);
    ....
}

多线程并发调用 rand、srand 等函数会造成数据竞争,导致未定义的行为。

依据

ISO/IEC 14882:2011 17.6.4.10(1)-undefined

规则

ID_dataRaces
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.