Windows threading: _beginthread vs _beginthreadex vs CreateThread C++

后端 未结 17 1479
臣服心动
臣服心动 2020-11-22 17:26

What\'s a better way to start a thread, _beginthread, _beginthreadx or CreateThread?

I\'m trying to determine what are the adv

17条回答
  •  深忆病人
    2020-11-22 17:36

    You should try this code

    #include
    #include
    #include
    #include
    
    UINT __stdcall Staff(PVOID lp){
     printf("The Number is %d\n", GetCurrentThreadId());
     return 0;
    }
    
    INT main(INT argc, PCHAR argv[])
    {
    
        const INT Staff_Number = 5;
        HANDLE hd[Staff_Number];
        for(INT i=0; i < Staff_Number; i++){
           hd[i] = (HANDLE)_beginthreadex(NULL, 0, Staff, NULL, 0, NULL);
        }
    
     WaitForMultipleObjects(Staff_Number, Staff, TRUE, NULL);
     for(INT i=0; i < Staff_Number; i++)
     {
         CloseHandle(hd[i]);
     }
     system("pause");
     return 0;
    }
    

    if you use _beginthread instead of _beginthreadex it will give error too many argument for _beginthread it is because _beginthread couldnt create a thread with security attribute and also i think _beginthread is unnecessary you can absolutely use *(_beginthreadex) and CreateThread

提交回复
热议问题