"const &value"的值在for循环的每一次循环中是一样的(可以看一下生成的汇编代码,栈的布局),等于是多个线程读取了同一个地址上的栈变量。
vs2019 debug编译,ebp-24h就是&value的值。
int __stdcall _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
00D83520 push ebp
00D83521 mov ebp,esp
00D83523 sub esp,4DCh
00D83529 push ebx
00D8352A push esi
00D8352B push edi
00D8352C lea edi,[ebp-4DCh]
00D83532 mov ecx,137h
00D83537 mov eax,0CCCCCCCCh
00D8353C rep stos dword ptr es:[edi]
00D8353E mov eax,dword ptr [__security_cookie (0DB4404h)]
00D83543 xor eax,ebp
00D83545 mov dword ptr [ebp-4],eax
00D83548 mov ecx,offset _CCD774AE_main@cpp (0DBF0BBh)
00D8354D call @__CheckForDebuggerJustMyCode@4 (0CBBA2Ch)
for (int i = 0; i < 3; i++) {
00D83552 mov dword ptr [ebp-0Ch],0
00D83559 jmp wWinMain+44h (0D83564h)
00D8355B mov eax,dword ptr [ebp-0Ch]
00D8355E add eax,1
00D83561 mov dword ptr [ebp-0Ch],eax
00D83564 cmp dword ptr [ebp-0Ch],3
00D83568 jge wWinMain+0C4h (0D835E4h)
std::vector<int> value = { i, i + 1 }; //返回一个数组
00D8356A push 10h
00D8356C lea ecx,[ebp-24h]
00D8356F call std::vector<int,std::allocator<int> >::__autoclassinit2 (0CBCAD5h)
00D83574 mov eax,dword ptr [ebp-0Ch]
00D83577 mov dword ptr [ebp-4D4h],eax
00D8357D mov ecx,dword ptr [ebp-0Ch]
00D83580 add ecx,1
00D83583 mov dword ptr [ebp-4D0h],ecx
00D83589 lea ecx,[ebp-465h]
00D8358F call std::allocator<int>::allocator<int> (0CBCAE9h)
00D83594 push eax
00D83595 lea edx,[ebp-4CCh]
00D8359B push edx
00D8359C lea eax,[ebp-4D4h]
00D835A2 push eax
00D835A3 lea ecx,[ebp-478h]
00D835A9 call std::initializer_list<int>::initializer_list<int> (0CBCB16h)
00D835AE mov ecx,dword ptr [eax+4]
00D835B1 push ecx
00D835B2 mov edx,dword ptr [eax]
00D835B4 push edx
00D835B5 lea ecx,[ebp-24h]
00D835B8 call std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> > (0CBCB2Ah)
printf("%d\n", value[0]);
00D835BD push 0
00D835BF lea ecx,[ebp-24h]
00D835C2 call std::vector<int,std::allocator<int> >::operator[] (0CBCADAh)
00D835C7 mov eax,dword ptr [eax]
00D835C9 push eax
00D835CA push offset string "%d\n" (0DA44B4h)
00D835CF call _printf (0CBCAA8h)
00D835D4 add esp,8
}
00D835D7 lea ecx,[ebp-24h]
00D835DA call std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> > (0CBCB0Ch)
00D835DF jmp wWinMain+3Bh (0D8355Bh)
【 在 here080 的大作中提到: 】
: 只要在threadpool完成运行之前保持生命周期就行。
:
--
FROM 123.118.66.*