Skip to content

Commit

Permalink
Implement Win32 threading functions
Browse files Browse the repository at this point in the history
  • Loading branch information
oold authored and jtsiomb committed Jan 3, 2023
1 parent e3d46ff commit 3124713
Show file tree
Hide file tree
Showing 9 changed files with 1,942 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/test.o
/test
35 changes: 23 additions & 12 deletions README
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
Trivial C11 threads.h implementation over POSIX threads.
Trivial C11 threads.h implementation over POSIX threads and not-so-trivial
implementation over Win32 threads.

Rationale
---------
GNU libc doesn't implement the thread part of the C library from the recent C11
standard, even though recent versions of gcc do support other aspects of it
(like atomics).
(like atomics). Likewise, Microsoft's VCRT also doesn't implement C11 threading.

If you're starting a new multithreaded project in C right now, it would make
sense to use the standard C way of using threads instead of a mismash of
sense to use the standard C way of using threads instead of a mishmash of
various platform-specific APIs.

So until the system libc adds support for it, we need a stopgap that works
exactly as the C standard describes.

exactly as the C standard describes, though on a best effort basis for Win32,
where the system API doesn't quite match the requirements.

How to use
----------
No installation, no compilation, just drop c11threads.h to your project, and
don't forget to link with -lpthread

For pthread users:
No installation, no compilation, just drop c11threads.h into your project, and
don't forget to link with "-lpthread".
This library is extremely thin and implemented directly in the header file as a
bunch of "static inline" functions (you do have at least C99 functionality while
trying to use C11 threads, right?).
trying to use C11 threads, right?). Define "C11THREADS_PTHREAD_WIN32" if you are
compiling for Win32.

For non-pthread users on Windows:
You need to compile c11threads_win32.c and link with it. The library works with
Windows NT 4.0 or higher and uses some shims selected at runtime if running on
Windows versions prior to Vista. Call "c11threads_destroy_win32()" after you are
done using the library or you might leak resources.

If you'd like to use this with a C89 compiler, just search&replace or
define-away all instances of "inline" to oblivion. Each object file will get a
Expand All @@ -30,13 +38,16 @@ reasonably modern system (say anything after the 80s).

License
-------
Author: John Tsiombikas <nuclear@member.fsf.org>
Authors:
John Tsiombikas <nuclear@member.fsf.org>
Oliver Old <oliver.old@outlook.com>

I place this piece of code in the public domain. Feel free to use as you see
fit. I'd appreciate it if you keep my name at the top of the code somehwere, but
fit. I'd appreciate it if you keep my name at the top of the code somewhere, but
whatever.

Main project site: https://github.com/jtsiomb/c11threads

Feel free to send corrections, patches, trendy social pull requests,
pictures of your cat wearing santa hats, any good porn links, or investment
opportunities with nigerian ex-royals... it's all good.
opportunities with Nigerian ex-royals... It's all good.

0 comments on commit 3124713

Please sign in to comment.