C11 threads on Windows

c11 threads vs pthreads
c11 standard thread
thread h library
c11 threads example
std::thread
c++ thread primitives
threads h not found
thread library

I'm creating cross platform software in Visual Studio 2012 express on Windows. For obvious reasons I can't use .NET's System::Threading::Thread. I was hoping I could use the new threading features of C11 (threads.h, not pthread.h), while using VS2012 since the I created a abstract framework based on .NET forms. I'm starting to believe that it's impossible for Windows. Does someone have an idea. I'll only use C++ libraries (boost and std) if those are my only options.

Is there someone who knows what to do?

Visual Studio 2012 doesn't support C11's threading (Microsoft has stated repeatedly that it has little interest in keeping current with C, preferring to focus on C++), but it does support C++11's std::thread and related facilities. If you're writing C++, you should arguably be using them anyways instead of C's threading libraries.

TinyCThread, The “other platform” for threads that clearly had been targeted by the committee are threads on Microsoft Windows platforms. Most other widely� The C11 thread interface was mostly copied from Dikumware's thread interface in their propretary thread library. AFAIR their stuff runs on different platforms and they have created that interface as an intersection of the functionalities of Windows threads and POSIX threads.

Visual Studio 2017 contains a header xthreads.h which is very similar but slightly different fromthreads.h. For example:

from https://en.cppreference.com/w/c/thread/thrd_sleep

#include <threads.h>
#include <time.h>
#include <stdio.h>

int main(void)
{
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
    thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
}

Would be

#include <thr/xthreads.h>
#include <time.h>
#include <stdio.h>

int main(void)
{
    struct xtime stoptime;
    xtime_get( &stoptime, 1);
    stoptime.sec += 1;
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
    _Thrd_sleep( &stoptime ); 
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
}

* NOTE: xthreads.h IS NOT standard and therefore subject to change. *

There is also an emulation library at https://gist.github.com/yohhoy/2223710 .

C11 defects: C threads are not realizable with POSIX threads – Jens , Compilers / (c) libs don't support it yet as much. Tinycthread does a well job of emulating it on top of pthread API or whatever is on windows. Not� C11 <threads.h> emulation library. GitHub Gist: instantly share code, notes, and snippets.

The C11 thread interface was mostly copied from Dikumware's thread interface in their propretary thread library. AFAIR their stuff runs on different platforms and they have created that interface as an intersection of the functionalities of Windows threads and POSIX threads.

Whether or not they have that now as "official" C11 thread library, I don't know, but it shouldn't be far from it.

C11 Threads [discussion] : C_Programming, But threads.h is C11 compliant so by now ALL compilers have I'm pretty sure glibc doesn't), although I'm not sure what Windows does. Need portable threads for your C app? Is C11 unavailable for your target compiler(s)? Then you need TinyCThread! TinyCThread implements a fairly compatible subset of the C11 thread management functions. Features. Open source! Very portable (designed to work under Windows, Mac OS X and Linux, and should work under most POSIX compatible systems).

pthread is outdated since availability of C11 which introduced , #define EMULATED_THREADS_H_INCLUDED_. #include <time.h>. #if defined (_WIN32) && defined(_MSC_VER). #include <windows.h>. C11 mainly standardizes features already supported by common contemporary compilers, and includes a detailed memory model to better support multiple threads of execution. Due to delayed availability of conforming C99 implementations, C11 makes certain features optional, to make it easier to comply with the core language standard.

C11 <threads.h> emulation library � GitHub, h implementation over POSIX threads. Rationale --------- GNU libc doesn't implement the thread part of the C library from the recent C11 standard, even� Microsoft Windows operating system's support for multithreaded programming is almost similar to the support provided by POSIX threads. The differences are not in the actual functionality but the names in the API functions.

jtsiomb/c11threads: Trivial C11 threads.h implementation , Every time I want/need to port some C code from Linux to Windows I have to IIRC, there is already a complete C11 thread.h underlying Microsoft's C++11� Creating Threads. 05/31/2018; 4 minutes to read; In this article. The CreateThread function creates a new thread for a process. The creating thread must specify the starting address of the code that the new thread is to execute.

Comments
  • Visual Studio does not generally support C, except those features which it shares with C++. And I don't think they plan on supporting it any time soon, if ever.
  • As far as I know, the C11 threading library is not implemented in MSVC. (They still have a way to go for C99 compliance.) Since you are on VS2012, you would have much better luck with the C++11 threading library which is available to you.
  • You're probably right.
  • +1 for noticing the OP mentioned C11 and not C++11. i would have answered as if the OP were wrong about C++11 threading support in Visual C++...
  • Thanks, the reason I prefere C11 is for future compatibility with C, if I ever decide to increase the performance. But std indeed has a lot of advantages though.
  • "future compatibility with C, if I ever decide to increase the performance" ...Huh? Well-written C++ can be just as performant as C. The C vs. C++ isn't a performance one (at least not with modern compilers), but one about programming style and methodologies.
  • The difference between C and C++ is indeed very small these days, but OOP and Generic programming can't keep up with procedural. But you're right about the methodologies. I actually don't like that (for example) more low level applications are written in languages like Java and HTML5/Javascript. First you should make the things you have as good as possible before depending on external factors (faster CPU). This only hides the lower quality coding. But I guess that is where we're heading.
  • C++ can even be faster than C and produce smaller binaries if properly written. There is no such thing as ÖOP style programs are slower" nowday. This was probably an issue at the beginning, whn C++ was a construction on top of C, but in 2016 this is not relevant anymore. "OOP and generic programming can't keep up with procedural" is a false statement.
  • This header file <thr/xthreads.h> is undocumented and therefore subject to incompatible changes. For an example, see developercommunity.visualstudio.com/content/problem/413586/…
  • Thanks, I added a NOTE to let others know.