Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do if <threads.h> isn't recognize (in C11)?

I've been learning how to do multi-threading in C and wanted to use <threads.h>. But it (my compiler GCC 8.1) says that <threads.h> isn't a file or a directory when I wanted to compile this code :

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>

int main(void) {
     printf("Hello World\n");
     return EXIT_SUCCESS;
}

So yeah... a hello world program but at the compile-time it doesn't work. Help me please. Also I am using Code::Blocks.

like image 410
Hemdy Mameche Avatar asked Sep 03 '25 14:09

Hemdy Mameche


2 Answers

The threads.h library doesn't have wide support among compilers, as most OSs have their own threading implementation.

Since you're using GCC, try using pthreads instead. If you're using the MinGW variant on Windows, you'll need to use the Windows threading functions instead.

like image 69
dbush Avatar answered Sep 05 '25 16:09

dbush


GNU Gnulib defines thread.h, which is documented to support MinGW (Minimalist GNU for Windows).  MinGW includes a GCC distribution.

See the Gnulib thread.h documentation.

I have not tried this myself, but you may find it helpful.

like image 39
Matthew Flaschen Avatar answered Sep 05 '25 15:09

Matthew Flaschen