我有以下平行的幻灯:
#include <omp.h>
#include "stdio.h"
int main()
{
omp_set_num_threads(4);
int i;
#pragma omp parallel private(i)
{
#pragma omp for
for(i = 0;i < 10; i++) {
printf("A %d: %d
", omp_get_thread_num(),i);
}
#pragma omp critical
printf("i %d: %d
", omp_get_thread_num(), i );
}
}
我认为,在航程之后,每一条路面都将有<条码>i条码>,等于<条码>i条码>。 产出如下:
A 0: 0
A 0: 1
A 0: 2
A 3: 9
A 2: 6
A 2: 7
A 2: 8
A 1: 3
A 1: 4
A 1: 5
i 0: 3
i 3: 10
i 2: 9
i 1: 6
我得到的是:
A 0: 0
A 0: 1
A 0: 2
A 3: 9
A 2: 6
A 2: 7
A 2: 8
A 1: 3
A 1: 4
A 1: 5
i 0: -1217085452
i 3: -1217085452
i 2: -1217085452
i 1: -1217085452
如何使<条码>i以保持最后的代谢价值?last private(i)
,i = 10
for all threads, that is not what 我想.