为什么我的程序找不到 pthread_barrier_init.c 文件?



在学校里,我正在做一个项目,该项目有两个正在运行的读取线程和一个围绕共享缓冲区工作的写入线程。这个共享缓冲区是我们自己编程的某种基于指针的列表。为了保证线程安全,我使用了pthread_rw_locks和一些pthread_barriers。当我试图运行我的代码时,它几乎立即崩溃,并给我一个分段错误。当使用gdb调试器时,它给了我以下消息:

程序接收到信号SIGSEGV,分段故障。

__pthread_barrier_init.c:47 处的pthread_barrier_init(barrier=0x0,attr=0x0,计数=2(

47 pthread_barrier_init.c:没有这样的文件或目录。

在编译时,我包含了-lpthread标志,并确保在使用pthread.h的每个文件中都包含pthread.h。知道为什么我的程序找不到这个c文件吗?

编辑

这是我使用的代码片段。(这几乎是所有的代码,但在这一部分出现了错误(

这是我的主循环代码

int main(int argc, char*argv[])
{
sbuffer_t* buffer;
sbuffer_init(&buffer);
return 0;
}

这是我的缓冲代码

/**
* basic node for the buffer, these nodes are linked together to create the buffer
*/
typedef struct sbuffer_node {
struct sbuffer_node *next;  /**< a pointer to the next node*/
sensor_data_t data;         /**< a structure containing the data */
} sbuffer_node_t;
/**
* a structure to keep track of the buffer
*/
struct sbuffer {
sbuffer_node_t *head;       /**< a pointer to the first node in the buffer */
sbuffer_node_t *tail;       /**< a pointer to the last node in the buffer */
pthread_rwlock_t* lock; 
pthread_barrier_t* barrierRead; //Barrier to indicate that both reader threads have succesfully read the sensor reading
pthread_barrier_t* barrierWrite; //Barrier to indicate that a sensor reading has been removed
pthread_mutex_t* FIFOlock;
int finished;
};
int sbuffer_init(sbuffer_t **buffer) {
(*buffer) = malloc(sizeof(sbuffer_t));
(*buffer)->lock=malloc(sizeof(pthread_rwlock_t));
if (*buffer == NULL) return SBUFFER_FAILURE;
pthread_rwlock_init((*buffer)->lock,NULL);
pthread_rwlock_wrlock((*buffer)->lock); 
pthread_barrier_init((*buffer)->barrierRead, NULL, READER_THREADS);
pthread_barrier_init((*buffer)->barrierWrite, NULL, READER_THREADS);
pthread_mutex_init((*buffer)->FIFOlock, NULL);
(*buffer)->head = NULL;
(*buffer)->tail = NULL;
(*buffer)->finished = CONNMGR_NOT_FINISHED;
pthread_rwlock_unlock((*buffer)->lock);
return SBUFFER_SUCCESS;
}

错误不是错误,只是警告,它不是由程序发出的,而是由调试器发出的。调试器试图通过显示发生崩溃的源文件来帮助您。遗憾的是,那个源文件不是程序的一部分,而是pthreads库的一部分。由于它不可用,调试器会通知您这一事实,否则您可能会看到问题所在的源代码行。gdb有一个"显示源代码行"函数,它在引发信号/异常后被调用,该函数将始终打印一些内容:源代码行或警告消息。

最新更新