C11 threads on Windows
Asked Answered
S

4

7

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?

Spastic answered 28/2, 2013 at 17:37 Comment(3)
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.Ionium
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.Attested
You're probably right.Spastic
L
12

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.

Lyda answered 28/2, 2013 at 17:46 Comment(5)
+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++...Boudreau
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.Spastic
"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.Lyda
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.Spastic
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.Shook
E
7

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 .

Explanation answered 20/2, 2019 at 17:12 Comment(2)
This header file <thr/xthreads.h> is undocumented and therefore subject to incompatible changes. For an example, see developercommunity.visualstudio.com/content/problem/413586/…Menam
Thanks, I added a NOTE to let others know.Explanation
C
2

It seems that meanwhile Microsoft has implemented the <threads.h> library in vs2022. Threading support

Cropdusting answered 13/8, 2024 at 14:40 Comment(0)
K
0

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.

Keitt answered 28/2, 2013 at 19:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.