时间:2016-09-09 06:33:45

标签: multithreading openmp

这是我的测试代码。我用vs2008在Windows 7中测试了这段代码。首先我创建一个线程(A),然后在线程(A)中我调用openmp进行一些优化。当线程(A)关闭时,openmp创建的线程不是空闲的。此过程中的线程数不断增加。任何人都可以告诉为什么以及如何解决它。感谢^^!

#include <windows.h>
#include <iostream>
#include <omp.h>

using namespace std;

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    #pragma omp parallel for
    for (int i = 0; i < 5; ++ i)
    {
        cout << "Child thread: i = " << i << endl;
    }

    return 0L;
}

int main()
{
    for (int i = 0; i < 100; ++i)
    {
        HANDLE thread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);

        DWORD   dwExitCode;
        WaitForSingleObject( thread, INFINITE );
        GetExitCodeThread(thread, &dwExitCode);
        if (dwExitCode == STILL_ACTIVE)
        {
            printf("current thread still running!\n");
            break;
        }
    }

    for (int i = 0; i < 5; ++ i)
    {
        cout << "main thread: i = " << i << endl;
    }

    system("pause");
    return 0;
}

0 个答案:

没有答案