两个线程执行i++100次,结果不是不一定吗?为什么我这个代码,一直是200?哪里写错了,求指导

问题遇到的现象和发生背景

两个线程执行i++100次,结果不是不一定吗?为什么我这个代码,一直是200?哪里写错了,求指导

问题相关代码,请勿粘贴截图
void thread01(int* num)
{
    for (int i = 0; i < 100; i++)
    {
        (*num)++;
        cout <<"thread01:-----"<< *num << endl;
        Sleep(100);
    }

}
void thread02(int* num)
{
    for (int i = 0; i < 100; i++)
    {
        (*num)++;
        cout << "thread02:-----" << *num << endl;
        Sleep(100);
    }
}
static int count11 = 0;
int main()
{
    
    thread task01(thread01, &count11);  //带参数子线程
    thread task02(thread02, &count11);
    task01.join();
    task02.join();
    cout << "结果:"<< count11 << endl;
    return 0;
}

把Sleep(100);注释掉

你可以参考下文


如有帮助,望采纳!