简体   繁体   中英

unknown type name “pthread_barrier_t”

I am trying to parallelize an algorithm in C. I want to use pthread_barrier_t but my Ubuntu wsl can't find it for some reason. I have pthread.h included and I can use the rest of the pthread functions. libthread.a is installed.

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

/* Error occurs here */
pthread_barrier_t barrier;

Exact error is: "identifier pthread_barrier_t is undefined"

I saw elsewhere it could be the way I'm compiling.

Compiling as follows:

gcc -o test test.c -Wall -std=c99 -lpthread -lm

Also, VS Code can't identify the function.

The problem is your -std=c99 option. Using strict C mode disables a bunch of stuff, including something that stops pthread_barrier_t from getting defined. If you use -std=gnu99 instead, it should compile. (Tested on Ubuntu 16.04 on WSL).

Alternatively, add

#define _XOPEN_SOURCE 600 /* Or higher */

or

#define _POSIX_C_SOURCE 200112L /* Or higher */

before the first #include in your source. Seeman 7 feature_test_macros for the acceptable values of these macros and more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM