git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3] win32: fix thread usage for win32

From
RGRose via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 23, 2023, 16:46 UTC
Message-ID
<pull.1440.v3.git.git.1674492373925.gitgitgadget@gmail.com>
In-Reply-To
<pull.1440.v2.git.git.1674491796648.gitgitgadget@gmail.com>
From: Seija Kijin <doremylover123@gmail.com>

Use _beginthreadex instead of CreateThread since we use the Windows CRT, as Microsoft recommends _beginthreadex over CreateThread for these situations.

Finally, check for NULL handles, not "INVALID_HANDLE," as _beginthreadex guarantees a valid handle in most cases

Signed-off-by: Seija Kijin <doremylover123@gmail.com>
---
    win32: fix thread usage for win32
    
    Use pthread_exit instead of async_exit.
    
    This means we do not have to deal with Windows's implementation
    requiring an unsigned exit coded despite the POSIX exit code requiring a
    signed exit code.
    
    Use _beginthreadex instead of CreateThread since we use the Windows CRT.
    
    Finally, check for NULL handles, not "INVALID_HANDLE," as _beginthreadex
    guarantees a valid handle in most cases
    
    Signed-off-by: Seija Kijin doremylover123@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v3
Pull-Request: https://github.com/git/git/pull/1440
Range-diff vs v2:
 1:  4a2c3da9d4c ! 1:  68baafba2bd win32: fix thread usage for win32
     @@ Commit message
          win32: fix thread usage for win32
      
          Use _beginthreadex instead of CreateThread
     -    since we use the Windows CRT.
     +    since we use the Windows CRT,
     +    as Microsoft recommends _beginthreadex
     +    over CreateThread for these situations.
      
          Finally, check for NULL handles, not "INVALID_HANDLE,"
          as _beginthreadex guarantees a valid handle in most cases
 compat/mingw.c   | 2 +-
 compat/winansi.c | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index e433740381b..715f1c87e11 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -2291,7 +2291,7 @@ static int start_timer_thread(void)
 	timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);
 	if (timer_event) {
 		timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);
-		if (!timer_thread )
+		if (!timer_thread)
 			return errno = ENOMEM,
 				error("cannot start timer thread");
 	} else
diff --git a/compat/winansi.c b/compat/winansi.c
index 3abe8dd5a27..be65b27bd75 100644
--- a/compat/winansi.c
+++ b/compat/winansi.c
@@ -340,7 +340,7 @@ enum {
 	TEXT = 0, ESCAPE = 033, BRACKET = '['
 };
 
-static DWORD WINAPI console_thread(LPVOID unused)
+static unsigned int WINAPI console_thread(LPVOID unused)
 {
 	unsigned char buffer[BUFFER_SIZE];
 	DWORD bytes;
@@ -643,9 +643,9 @@ void winansi_init(void)
 		die_lasterr("CreateFile for named pipe failed");
 
 	/* start console spool thread on the pipe's read end */
-	hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
-	if (hthread == INVALID_HANDLE_VALUE)
-		die_lasterr("CreateThread(console_thread) failed");
+	hthread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);
+	if (!hthread)
+		die_lasterr("_beginthreadex(console_thread) failed");
 
 	/* schedule cleanup routine */
 	if (atexit(winansi_exit))

base-commit: 56c8fb1e95377900ec9d53c07886022af0a5d3c2
-- 
gitgitgadget
Previous: Rose via GitGitGadgetNext: Rose via GitGitGadget
Message 4 of 11 in “win32: fix thread usage for win32”
  1. win32: fix thread usage for win32Rose via GitGitGadget, Jan 21, 2023
  2. Johannes SixtJan 21, 2023
  3. win32: fix thread usage for win32Rose via GitGitGadget, Jan 23, 2023
  4. win32: fix thread usage for win32Rose via GitGitGadget, Jan 23, 2023
  5. win32: fix thread usage for win32Rose via GitGitGadget, Jan 23, 2023
  6. Jeff HostetlerJan 23, 2023
  7. Johannes SixtJan 23, 2023
  8. win32: fix thread usage for win32Rose via GitGitGadget, Jan 31, 2023
  9. Johannes SixtJan 31, 2023
  10. win32: fix thread usage for win32Rose via GitGitGadget, Feb 10, 2023
  11. win32: prefer beginthreadex over CreateThreadRose via GitGitGadget, Feb 10, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.