Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable C11 threads in MSVC 17.8

I was happy to hear that C11 threads were finally implemented in Visual Studio 2022 version 17.8 Preview 2.

Unfortunately, while the DLLs are there, I'm unable to find the header anywhere. I installed Visual Studio with the default options for C++ development. I'm using the latest Preview (7) with Windows 11 SDK 10.0.22621.0.

Does anyone have any clue on how to enable them?

like image 712
Costantino Grana Avatar asked Sep 13 '25 03:09

Costantino Grana


1 Answers

tl;dr; It seems they did implement some C11 threads support, but didn't ship a threads.h.

UPDATE: Someone from microsoft attached the real threads.h in the support thread https://developercommunity.visualstudio.com/t/Missing-threadsh-in-MSVC-178/10514752

I did some digging, and while they apparently did not ship a threads.h header, they did implement part of the C11 standard. I discovered this by writing my own header file, based on xthreads.h (the internal C++ thread header). From what I've read, the C++ standard heavily influenced the C version of the threads library, so I assumed the API would be similar.

This is the header file I created, it's not complete, it doesn't have mutexes or conditions in it. But it does have the standard thread functions thrd_create and thrd_join.

It seems microsoft did not implement thrd_sleep, I get "unresolved symbols" if I try to define thrd_sleep.

#ifndef _THREADS_H
#define _THREADS_H

#include <time.h>

typedef unsigned int _Thrd_id_t;

struct thrd_t { // thread identifier for Win32
    void* _Hnd; // Win32 HANDLE
    _Thrd_id_t _Id;
};
typedef struct thrd_t thrd_t;

enum thrd_result { 
  thrd_success = 0,
  thrd_nomem = 1,
  thrd_timedout = 2,
  thrd_busy = 3,
  thrd_error = 4
};

/** THIS SECTION IS COPIED FROM GNU LIB C **/
/** Copyright (C) 2018-2023 Free Software Foundation, Inc. **/
typedef int (*thrd_start_t) (void*);
/* Create a new thread executing the function __FUNC.  Arguments for __FUNC
   are passed through __ARG.  If successful, __THR is set to new thread
   identifier.  */
extern int __cdecl thrd_create (thrd_t *__thr, thrd_start_t __func, void *__arg);

/** END GNU LIBC SECTION **/

extern int __cdecl thrd_join(thrd_t, int*);

#endif

I used the above header in a test program I have, and it successfully linked to thrd_create and thrd_join functions, and it does work to create threads!

like image 91
Daniel Garcia Avatar answered Sep 15 '25 00:09

Daniel Garcia