C11 threads on Windows

10,282

Solution 1

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.

Solution 2

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 .

Share:
10,282
JMRC
Author by

JMRC

Just some guy with a computer.

Updated on June 08, 2022

Comments

  • JMRC
    JMRC almost 2 years

    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?

  • Cheers and hth. - Alf
    Cheers and hth. - Alf about 11 years
    +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++...
  • JMRC
    JMRC about 11 years
    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.
  • Matt Kline
    Matt Kline about 11 years
    "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.
  • JMRC
    JMRC about 11 years
    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.
  • ingframin
    ingframin about 8 years
    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.
  • Bruno Haible
    Bruno Haible almost 5 years
    This header file <thr/xthreads.h> is undocumented and therefore subject to incompatible changes. For an example, see developercommunity.visualstudio.com/content/problem/413586/…
  • annoying_squid
    annoying_squid almost 5 years
    Thanks, I added a NOTE to let others know.