嗨,我是 OpenMp 的新手,并且遇到了简单的 for 循环问题。这是我的代码:
#include "stdafx.h"
#include <Windows.h>
#include <omp.h>
char str[20];
INT64 i;
int main()
{
HANDLE hAppend;
DWORD dwBytesToWrite ,dwPos;
DWORD dwBytesWritten = 0;
hAppend = CreateFile(L"log.txt" ,
GENERIC_WRITE,
0, // do not share
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
#pragma omp parallel num_threads(4) private(i)
{
#pragma omp for ordered
for( i=0; i<=MAXINT64;i++)
{
sprintf(str,"%x | %x \r\n",i);
dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
LockFile(hAppend, dwPos, 0, strlen(str), 0);
WriteFile(
hAppend,
str,
strlen(str),
&dwBytesWritten,
NULL
);
}
}
}
这是输出的一部分:
1ee5 | 60000000
1ee6 | 60000000
1ee7 | 60000000
1ee8 | 60000000
1ee9 | 60000000
1eea | 60000000
0 | | 60000000
0 | 40000000
1eed | 600000001 | 40000000
2 | 40000000
1 | 40000000
1eee | 60000000
1eef | 60000000
1ef0 | 60000000
1ef1 | 60000000
3 | 40000000
1ef 40000000
4 | 40000000
2 | 0
3 | 0
4 | 0
没有 open mp 一切正常,但并行输出变得错误。OpenMp 有什么问题?或我的代码?