source

C11 GCC threads.h not found?

ittop 2023. 10. 14. 10:37
반응형

C11 GCC threads.h not found?

The following code

#include <threads.h>

Gives me this error:

fatal error: threads.h: No such file or directory

Using the latest GCC and Clang with -std=c11.

Is C11 threading not supported by GCC and Clang? Or is there a hack (or something to install) to get it? I'm just using Ubuntu 14.04 with the gcc and clang packages from the Ubuntu repo.

gcc문서 C11 상태는 쓰레드를 지원하지 않는다는 것을 나타냅니다.

Threading [Optional] | Library issue (not implemented)

문서에서 알 수 있듯이 이것은 실제로는 아닙니다.gcc아니면clang발행.glibc문제입니다. 잭이 지적한 바와 같이 이에 대한 지원을 받기 위한 작업이 곧 진행될 것으로 보입니다.glibc하지만 지금은 도움이 되지 않을 겁니다사이에 이걸 쓰셔도 됩니다.

Fixed for glibc 2.28

According the Bug 14092 - Support C11 threads this is fixed in glibc 2.28:

Implemented upstream by:

9d0a979 스레드에 대한 설명서 추가.h
0a07288 nptl:ISO C11 스레드에 대한 테스트 사례 추가
c6d669 nptl:C11 스레드에 대한 빌리스트 기호 추가
78d4013 nptl:C11 스레드 추가 tss_* 함수
918311 앤틀:C11 스레드 추가 cnd_* 함수
3c20a67 nptl:C11 스레드 call_once 함수 추가
18d59c1 nptl:C11 스레드 추가 mtx_* 함수
ce7528 fnptl:C11 스레드 추가 thrd_* 함수

It will be included in 2.28.

머슬받침 C11<threads.h>.

데비안 설치 시musl-tools, 다음과 같이 편집합니다.musl-gcc. 저는 Glibc 대신 Musl과 함께 Debian을 부트스트래핑하는 작업을 하고 있습니다.

Also see this.

While C11 threads has not been implemented yet, C++11 threads have been implemented and they have similar functionality. Of course, C++11 may be an unacceptable solution, in which case the prior comments about POSIX threads are your best hope.

Threads have been merged into mainline Glibc and are available for example on my Ubuntu 20.04. Unfortunately I don't seem to have any manual pages for the function. But this works:

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

int hello_from_threading(void *arg) {
    printf("Thread about to take a nap...\n");
    thrd_sleep(&(struct timespec) { .tv_sec = 3 }, NULL);
    printf("Woke up from 3 second slumber\n");
    return 42;
}

int main(void) {
    thrd_t thread;
    thrd_create(&thread, hello_from_threading, NULL);
    int res;
    printf("A thread was started\n");
    thrd_join(thread, &res);
    printf("Thread ended, returning %d\n", res);
}

and testing it:

% gcc threading.c -o threading -lpthread
% ./threading
A thread was started
Thread about to take a nap...
Woke up from 3 second slumber
Thread ended, returning 42

You can compile it with the command;

clang c-prog-with-threads_h.c

without using -lpthread now. (And latest versions of compilers did not need to specify std=c11, because of it is default).

clang version 14.0.1, platform: Termux app, Android.

ReferenceURL : https://stackoverflow.com/questions/22875545/c11-gcc-threads-h-not-found

반응형