this post was submitted on 03 Oct 2023
9 points (84.6% liked)

Programming

17308 readers
79 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
 

Without realizing what I was getting myself into, I wrote some code using C11's threads.h (EDIT: every time I use the angle brackets < and > they just get eaten, even in the code snippet block.) I'm realizing after the fact that this is basically only supported on Linux (gcc/clang). This is my target platform, but I guess if I could cross compile to Windows or macOS that would be nice, too.

C's threads nominally appear to be a great feature. Finally, a standardized and straightforward interface to threads that would be cross-platform compatible. The reality appears to be anything but.

So is it worth just replacing that code with pthreads? Is there some near-term development on C threads that might make this worthwhile to use? I'm kind of surprised it hasn't really caught on some 12 years after the standard was introduced.

all 7 comments
sorted by: hot top controversial new old
[–] [email protected] 5 points 1 year ago

pthreads is fine and widely supported. I would probably recommend that interface for now. Maybe after 5 years or so when all of the LTS systems support the new standard that will be the better option, but all major OSes are POSIX compatible so no need for a new standard anyways.

Also if you want to take advantage of any OS-specific extensions they are probably supported by pthreads, but the new C threads API will probably lag.

[–] [email protected] 2 points 1 year ago

c11 threads seem to be supported by msvc https://devblogs.microsoft.com/cppblog/c11-threads-in-visual-studio-2022-version-17-8-preview-2/

Probably gcc supports it on both mac and MS

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago) (2 children)

As far as I know, C's threads.h implementation (or std::thread for C++) is based on POSIX threads.

If you're using CMake or a similar build system that can define macros when building from Windows, then one option you have is to simply create an interface of sorts.
Something like:

// angle brackes work for this codeblock? idk

#ifdef PLATFORM_WIN32
   // The syntax MSVC wants (which I'm not familiar with)
   #include "fuckmissingbracketsidontrememberwhatiwrotehere"
   thread_t win32_create_thread( /* ... */ ) {
      /* ... */
   }
#else
   // The syntax sensible compilers want (which I'm also unfamiliar with because I forgor U+1F480)
   #include "fuckmissingbracketsidontrememberwhatiwrotehere"
   int thrd_create( /* ... */ ) {
      /* ... */
   }
#endif

It may be a bit tedious, but I don't know if there is some widely known C equivalent to the Boost library, at least for threads.

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago) (1 children)

Yeah. I think I'll end up having to do platform-specific ifdefs with either pthreads or threads.h, so I guess I may as well use the much better established pthreads and get macOS support by default. In fact, I just now learned that even glibc didn't support C11 threads until 2018, according to this https://sourceware.org/bugzilla/show_bug.cgi?id=14092#c10

[–] [email protected] 2 points 1 year ago (1 children)

Sadly. angle brackes do not work for that codeblock.

[–] [email protected] 1 points 1 year ago

I swear they did in the preview