C11 threads on Windows
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 .
Comments
-
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
, notpthread.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 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 about 11 yearsThanks, 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 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 about 11 yearsThe 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 about 8 yearsC++ 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 almost 5 yearsThis 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 almost 5 yearsThanks, I added a
NOTE
to let others know.