我想在WinAPI中创建2个线程,这些线程将能够写入单个文本文件。我知道如何创建文件并将其写入。
#include <iostream>
#include <windows.h>
#include <string>
DWORD WINAPI firstthred(LPVOID lpParameter) {
// here I want one thread to write something in the txt file Something like this
HANDLE hFile = CreateFile(
L"C:\\Users\\Yon\\OneDrive\\Desktop\\example3.txt", FILE_WRITE_DATA, NULL, NULL, CREATE_NEW, NULL, NULL
);
char str[] = "Hello ";//Example text
DWORD bytesWritten;
WriteFile(hFile, str, strlen(str), &bytesWritten, NULL);
DWORD myThreadID;
}
DWORD WINAPI secondthred(LPVOID lpParameter) {
// here I want the other thread to write something different in the same txt file something like this
HANDLE hFile = CreateFile(
L"C:\\Users\\Yon\\OneDrive\\Desktop\\example3.txt", FILE_WRITE_DATA, NULL, NULL, CREATE_NEW, NULL, NULL
);
char str[] = "World"; //a diffrent Example of a text in the same file
DWORD bytesWritten;
WriteFile(hFile, str, strlen(str), &bytesWritten, NULL);
DWORD myThreadID;
}
int main()
{
HANDLE hThread = CreateThread(
NULL,
0,
firstthred,
NULL,
0,
&myThreadID
);
HANDLE hThread = CreateThread(
NULL,
0,
secondthred,
NULL,
0,
&myThreadID
);
}
如何创建2个写入文本文件的线程?
答案 0 :(得分:0)
首先,使用CREAT_NEW
标志,如果指定的文件存在,则函数失败。因此,如果两个线程都在运行,则两个线程中总会有CreateFile
失败,或者两个线程都失败。您可以在第一个线程中使用CREATE_ALWAYS
,在第二个线程中使用OPEN_ALWAYS
。
FILE_SHARE_WRITE
。
但这将导致您的一个输入被另一个线程覆盖。这是因为
每次打开文件时,系统都会将文件指针放在 文件的开头,偏移量为零。
除非您按以下顺序编写:
" World"
"Hello"
所以关键是控制文件指针。在您的示例中,您只需要同步写入顺序,然后在第二次打开文件时将文件指针设置为文件末尾(SetFilePointer
)(无需进行任何错误检查,统一字符集)。>
#include <iostream>
#include <windows.h>
#include <string>
#include <tchar.h>
HANDLE hmutex;
DWORD WINAPI firstthred(LPVOID lpParameter) {
// here I want one thread to write something in the txt file Something like this
WaitForSingleObject(hmutex, INFINITE);
HANDLE hFile = CreateFile(
TEXT("Path\\example3.txt"), FILE_WRITE_DATA, NULL, NULL, CREATE_ALWAYS, NULL, NULL
);
TCHAR str[] = TEXT("Hello ");//Example text
DWORD bytesWritten;
WriteFile(hFile, str, _tcslen(str)*sizeof(TCHAR), &bytesWritten, NULL);
CloseHandle(hFile);
ReleaseMutex(hmutex);
return 0;
}
DWORD WINAPI secondthred(LPVOID lpParameter) {
// here I want the other thread to write something different in the same txt file something like this
WaitForSingleObject(hmutex, INFINITE);
HANDLE hFile = CreateFile(
TEXT("Path\\example3.txt"), FILE_WRITE_DATA, NULL, NULL, OPEN_ALWAYS, NULL, NULL
);
//SetEndOfFile(hFile);
SetFilePointer(hFile, 0, NULL, FILE_END);
TCHAR str[] = TEXT("World"); //a diffrent Example of a text in the same file
DWORD bytesWritten;
WriteFile(hFile, str, _tcslen(str)*sizeof(TCHAR), &bytesWritten, NULL);
CloseHandle(hFile);
ReleaseMutex(hmutex);
return 0;
}
int main()
{
hmutex = CreateMutex(NULL, false, NULL);
DWORD myThreadID[2] = { 0 };
HANDLE hThread[2] = { 0 };
hThread[0] = CreateThread(
NULL,
0,
firstthred,
NULL,
0,
&myThreadID[0]
);
hThread[1] = CreateThread(
NULL,
0,
secondthred,
NULL,
0,
&myThreadID[1]
);
WaitForMultipleObjects(2, hThread, TRUE, INFINITE);//Wait for the threads exit.
return 0;
}
如果您真正想做的是同时在每个线程中循环写入文件,则建议您只打开一个文件句柄并将其传递给lpParameter
。