CreateThread 的行为似乎类似于 fork()

发布于 2024-11-11 16:25:10 字数 3966 浏览 3 评论 0原文

我有这个函数

int APIENTRY _tWinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 LPTSTR    lpCmdLine,
                 int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG     msg;
HACCEL  hAccelTable;
int     i;
DWORD   dwThreadIdArray[NUM_THREADS]; //edited after first post
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
PARAM_PASSED *paramPassed = NULL;
std::ostringstream ss;
std::wstring str;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LSP3, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
    return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LSP3));

// Initialize GDI+ 
GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL); 

// Create event in order to let the other threads know
// when they can start posting message
hStartEvent = CreateEvent(  NULL,                   // default security attributes
                            TRUE,                   // manual-reset event
                            FALSE,                  // initial state is nonsignaled
                            TEXT("StartEvent"));    // object name

if(hStartEvent == NULL)
{
    ss << "_tWinMain: CreateEvent failed" << std::endl;
    str = string2wideString(ss.str().c_str()); 
    OutputDebugString(str.c_str());
    return 1;
}

// Create threads
for(i = 0; i < NUM_THREADS; i++)
{
    paramPassed = NULL;
    threadHandles[i] = CreateThread(NULL,                   // security context is default 
                                    0,                      // stack size is default
                                    rectAnalyzer,           // start routine is rectAnalyzer
                                    paramPassed,            // parameter pointer is paramPassed 
                                    0,                      // thread active
                                    &dwThreadIdArray[i]);   // variable used to store thread id
    if(threadHandles[i] == NULL)
    {
        ss << "_tWinMain: error CreateThread " << i << std::endl;
        str = string2wideString(ss.str().c_str()); 
        OutputDebugString(str.c_str());
    }
    else
    {
        ss << "_tWinMain: created thread " << i << std::endl;
        str = string2wideString(ss.str().c_str()); 
        OutputDebugString(str.c_str());
    }
}

// Let other threads start
SetEvent(hStartEvent);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

// Wait for all threads
WaitForMultipleObjects(NUM_THREADS, threadHandles, TRUE, INFINITE);

for(i = 0; i < NUM_THREADS; i++)
{
    CloseHandle(threadHandles[i]);
}

//Releases GDI+
Gdiplus::GdiplusShutdown(gdiplusToken);

return (int) msg.wParam;
}

并且得到这个输出:

_tWinMain: created thread 0
_tWinMain: created thread 0
_tWinMain: created thread 1
_tWinMain: created thread 0
_tWinMain: created thread 1
_tWinMain: created thread 2  
_tWinMain: created thread 0
_tWinMain: created thread 1
_tWinMain: created thread 2
_tWinMain: created thread 3
The thread 'Win32 Thread' (0x159c) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x12bc) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x102c) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1594) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x107c) has exited with code 1 (0x1).
The program '[2308] lsp3.exe: Native' has exited with code 0 (0x0).

为什么我得到线程 0...2 的多个输出?我不知道我是创建 4 个线程还是 10 个线程..而且最后我似乎创建了 4 个线程加上主线程..

I have this function

int APIENTRY _tWinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 LPTSTR    lpCmdLine,
                 int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG     msg;
HACCEL  hAccelTable;
int     i;
DWORD   dwThreadIdArray[NUM_THREADS]; //edited after first post
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
PARAM_PASSED *paramPassed = NULL;
std::ostringstream ss;
std::wstring str;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LSP3, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
    return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LSP3));

// Initialize GDI+ 
GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL); 

// Create event in order to let the other threads know
// when they can start posting message
hStartEvent = CreateEvent(  NULL,                   // default security attributes
                            TRUE,                   // manual-reset event
                            FALSE,                  // initial state is nonsignaled
                            TEXT("StartEvent"));    // object name

if(hStartEvent == NULL)
{
    ss << "_tWinMain: CreateEvent failed" << std::endl;
    str = string2wideString(ss.str().c_str()); 
    OutputDebugString(str.c_str());
    return 1;
}

// Create threads
for(i = 0; i < NUM_THREADS; i++)
{
    paramPassed = NULL;
    threadHandles[i] = CreateThread(NULL,                   // security context is default 
                                    0,                      // stack size is default
                                    rectAnalyzer,           // start routine is rectAnalyzer
                                    paramPassed,            // parameter pointer is paramPassed 
                                    0,                      // thread active
                                    &dwThreadIdArray[i]);   // variable used to store thread id
    if(threadHandles[i] == NULL)
    {
        ss << "_tWinMain: error CreateThread " << i << std::endl;
        str = string2wideString(ss.str().c_str()); 
        OutputDebugString(str.c_str());
    }
    else
    {
        ss << "_tWinMain: created thread " << i << std::endl;
        str = string2wideString(ss.str().c_str()); 
        OutputDebugString(str.c_str());
    }
}

// Let other threads start
SetEvent(hStartEvent);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

// Wait for all threads
WaitForMultipleObjects(NUM_THREADS, threadHandles, TRUE, INFINITE);

for(i = 0; i < NUM_THREADS; i++)
{
    CloseHandle(threadHandles[i]);
}

//Releases GDI+
Gdiplus::GdiplusShutdown(gdiplusToken);

return (int) msg.wParam;
}

and I get this output:

_tWinMain: created thread 0
_tWinMain: created thread 0
_tWinMain: created thread 1
_tWinMain: created thread 0
_tWinMain: created thread 1
_tWinMain: created thread 2  
_tWinMain: created thread 0
_tWinMain: created thread 1
_tWinMain: created thread 2
_tWinMain: created thread 3
The thread 'Win32 Thread' (0x159c) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x12bc) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x102c) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1594) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x107c) has exited with code 1 (0x1).
The program '[2308] lsp3.exe: Native' has exited with code 0 (0x0).

Why am I getting multiple output for thread 0...2 ? I don't know if I am creating 4 threads or 10 threads.. Moreover in the end it seems I created 4 threads plus primary thread..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-11-18 16:25:10

您没有清除循环中的 stringstream ss ,因此输出只是累积:

线程 0
线程 0 1
线程 0 1 2
线程 0 1 2 3

在执行每个输出后添加一个 ss.str("")

You are not clearing the stringstream ss in the loop, so the output just accumulates:

thread 0
thread 0 1
thread 0 1 2
thread 0 1 2 3

Add a ss.str("") after doing each output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文