Windows: use CRT-safe _beginthreadex() rather than CreateThread(); also remove unnecessary NULL check for realloc()

This commit is contained in:
vylsaz 2025-05-21 05:05:14 +00:00
parent 15bf932757
commit 7cdbe866da

View File

@ -1,4 +1,5 @@
#include <windows.h> #include <windows.h>
#include <process.h>
typedef struct { typedef struct {
HANDLE hndl; HANDLE hndl;
@ -6,7 +7,7 @@ typedef struct {
u64 len; u64 len;
} ThreadIO; } ThreadIO;
static DWORD WINAPI winThreadWrite(LPVOID arg0) { static unsigned int __stdcall winThreadWrite(void* arg0) {
DWORD dwResult = ERROR_SUCCESS; DWORD dwResult = ERROR_SUCCESS;
ThreadIO* arg = arg0; ThreadIO* arg = arg0;
HANDLE hndl = arg->hndl; HANDLE hndl = arg->hndl;
@ -24,7 +25,7 @@ static DWORD WINAPI winThreadWrite(LPVOID arg0) {
return dwResult; return dwResult;
} }
static DWORD WINAPI winThreadRead(LPVOID arg0) { static unsigned int __stdcall winThreadRead(void* arg0) {
DWORD dwResult = ERROR_SUCCESS; DWORD dwResult = ERROR_SUCCESS;
ThreadIO* arg = arg0; ThreadIO* arg = arg0;
HANDLE hndl = arg->hndl; HANDLE hndl = arg->hndl;
@ -40,9 +41,7 @@ static DWORD WINAPI winThreadRead(LPVOID arg0) {
dwResult = GetLastError(); dwResult = GetLastError();
break; break;
} }
char* newBuf = (rBuf == NULL)? char* newBuf = realloc(rBuf, (dwHasRead+dwRead)*sizeof(char));
calloc(dwHasRead+dwRead, sizeof(char)) :
realloc(rBuf, (dwHasRead+dwRead)*sizeof(char));
if (newBuf == NULL) { dwResult = GetLastError(); break; } if (newBuf == NULL) { dwResult = GetLastError(); break; }
rBuf = newBuf; rBuf = newBuf;
memcpy(&rBuf[dwHasRead], buf, dwRead); memcpy(&rBuf[dwHasRead], buf, dwRead);
@ -112,9 +111,9 @@ static DWORD winCmd(WCHAR* arg,
DWORD exitCode = -1; DWORD exitCode = -1;
HANDLE lpThreads[3]; HANDLE lpThreads[3];
lpThreads[0] = CreateThread(NULL, 0, winThreadWrite, (LPVOID)&data0, 0, NULL); lpThreads[0] = (HANDLE)_beginthreadex(NULL, 0, winThreadWrite, (void*)&data0, 0, NULL);
lpThreads[1] = CreateThread(NULL, 0, winThreadRead, (LPVOID)&data1, 0, NULL); lpThreads[1] = (HANDLE)_beginthreadex(NULL, 0, winThreadRead, (void*)&data1, 0, NULL);
lpThreads[2] = CreateThread(NULL, 0, winThreadRead, (LPVOID)&data2, 0, NULL); lpThreads[2] = (HANDLE)_beginthreadex(NULL, 0, winThreadRead, (void*)&data2, 0, NULL);
for (int i = 0; i < 3; ++i) { for (int i = 0; i < 3; ++i) {
if (lpThreads[i] == NULL) { dwResult = GetLastError(); goto error; } if (lpThreads[i] == NULL) { dwResult = GetLastError(); goto error; }