为什么openmp中的循环运行顺序?

Why does the loop in openmp run sequentially?

我在 openmp 中尝试 运行 调度示例,但它按顺序工作。

omp_set_num_threads(4);
#pragma omp parallel for schedule(static, 3)
    for (int i = 0; i < 20; i++)
    {
        printf("Thread %d is running number %d\n", omp_get_thread_num(), i);
    }

结果:

Thread 0 is running number 0
Thread 0 is running number 1
Thread 0 is running number 2
Thread 0 is running number 3
Thread 0 is running number 4
Thread 0 is running number 5
Thread 0 is running number 6
Thread 0 is running number 7
Thread 0 is running number 8
Thread 0 is running number 9
Thread 0 is running number 10
Thread 0 is running number 11
Thread 0 is running number 12
Thread 0 is running number 13
Thread 0 is running number 14
Thread 0 is running number 15
Thread 0 is running number 16
Thread 0 is running number 17
Thread 0 is running number 18
Thread 0 is running number 19

如何让代码并行工作?

我使用的是 Microsoft Visual Studio 2017。

在 Microsoft Visual Studio 中,默认情况下禁用 OpenMP 支持。您可以使用 /openmp 编译器选项启用它。

可以在项目属性中启用此选项,在 C/C++->Language->Open MP Support.