Error: 未知的类型名 pthread_rw_lock_t
1、问题介绍在学习读写锁相关知识时,编写一段简单的程序验证。结果编译未通过,报错提示:未知的类型名 pthread_rw_lock_t
Linux版本:
Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017
gcc版本:
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
在网上查到过说要定义宏开关等等,都没有效果。个人猜测或许是gcc版本 or Linux版本问题。
2、代码demo
/*
* @Description: multi-thread sync : read-write lock
* @Author:
* @version:
* @Date: 2023-10-16 10:01:24
* @LastEditors:
* @LastEditTime: 2023-10-16 10:31:11
*/
//==============================================================================
// Include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
//==============================================================================
// Constants
#define MAX_N_WRITE 3
#define MAX_N_READ 5
#define MAX(a,b) ((a)>(b) ? (a):(b))
#define MIN(a,b) ((a)<(b) ? (a):(b))
//==============================================================================
// types
struct RW_DATA_ST
{
pthread_rw_lock_t rwlock;
int number;
};
//==============================================================================
// global varibles
struct RW_DATA_ST g_rw_data;
//==============================================================================
// global functions
void *thread_write(void *arg);
void *thread_read(void *arg);
//==============================================================================
// The main entry-point function.
int main(int argc, char **argv)
{
exit(0);
}
void *thread_write(void *arg)
{
return (NULL);
}
void *thread_read(void *arg)
{
return (NULL);
}
你好,这个错误是由于缺少pthread库导致的。你需要在编译时链接pthread库,可以在编译时加上-lpthread参数来链接该库。修改代码如下:
/*
* @Description: multi-thread sync : read-write lock
* @Author:
* @version:
* @Date: 2023-10-16 10:01:24
* @LastEditors:
* @LastEditTime: 2023-10-16 10:31:11
*/
//==============================================================================
// Include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
//==============================================================================
// Constants
#define MAX_N_WRITE 3
#define MAX_N_READ 5
#define MAX(a,b) ((a)>(b) ? (a):(b))
#define MIN(a,b) ((a)<(b) ? (a):(b))
//==============================================================================
// types
struct RW_DATA_ST
{
pthread_rwlock_t rwlock;// 修改此处
int number;
};
//==============================================================================
// global varibles
struct RW_DATA_ST g_rw_data;
//==============================================================================
// global functions
void *thread_write(void *arg);
void *thread_read(void *arg);
//==============================================================================
// The main entry-point function.
int main(int argc, char **argv)
{
pthread_rwlock_init(&g_rw_data.rwlock, NULL);// 初始化读写锁
pthread_t write_tid;
pthread_t read_tid;
int i = 0;
for (i = 0; i < MAX_N_WRITE; i++)
{
pthread_create(&write_tid[ i], NULL, thread_write, (void*)i);
}
for (i = 0; i < MAX_N_READ; i++)
{
pthread_create(&read_tid[ i], NULL, thread_read, (void*)i);
}
for (i = 0; i < MAX_N_WRITE; i++)
{
pthread_join(write_tid[ i], NULL);
}
for (i = 0; i < MAX_N_READ; i++)
{
pthread_join(read_tid[ i], NULL);
}
pthread_rwlock_destroy(&g_rw_data.rwlock);// 销毁读写锁
exit(0);
}
void *thread_write(void *arg)
{
int i = 0;
for (i = 0; i < 10; i++)
{
pthread_rwlock_wrlock(&g_rw_data.rwlock);// 获取写锁
g_rw_data.number = i;
printf("write thread %d write number %d\n", (int)arg, g_rw_data.number);
pthread_rwlock_unlock(&g_rw_data.rwlock);// 释放写锁
sleep(1);
}
return (NULL);
}
void *thread_read(void *arg)
{
int i = 0;
for (i = 0; i < 10; i++)
{
pthread_rwlock_rdlock(&g_rw_data.rwlock);// 获取读锁
printf("read thread %d read number %d\n", (int)arg, g_rw_data.number);
pthread_rwlock_unlock(&g_rw_data.rwlock);// 释放读锁
sleep(1);
}
return (NULL);
}
编译时使用以下命令:
gcc -o test test.c -lpthread
注意:请根据实际需求修改代码中的读写锁的初始化和销毁位置。 您的问题是在尝试使用读写锁(pthread_rw_lock_t)时遇到了“未知的类型名 pthread_rw_lock_t”的错误。这个问题的原因是,pthread库中并没有定义pthread_rw_lock_t这个类型。在POSIX线程库中,读写锁的类型是pthread_rwlock_t,而不是pthread_rw_lock_t。
所以,您需要将代码中的pthread_rw_lock_t更改为pthread_rwlock_t。同时,您需要使用正确的函数来初始化、读取锁定、写入锁定、解锁和销毁读写锁。这些函数分别是pthread_rwlock_init、pthread_rwlock_rdlock、pthread_rwlock_wrlock、pthread_rwlock_unlock和pthread_rwlock_destroy。
以下是您的代码的修正版本:
/*
* @Description: multi-thread sync : read-write lock
* @Author:
* @version:
* @Date: 2023-10-16 10:01:24
* @LastEditors:
* @LastEditTime: 2023-10-16 10:31:11
*/
//==============================================================================
// Include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
//==============================================================================
// Constants
#define MAX_N_WRITE 3
#define MAX_N_READ 5
#define MAX(a,b) ((a)>(b) ? (a):(b))
#define MIN(a,b) ((a)<(b) ? (a):(b))
//==============================================================================
// types
struct RW_DATA_ST {
pthread_rwlock_t rwlock;
int number;
};
//==============================================================================
// global varibles
struct RW_DATA_ST g_rw_data;
//==============================================================================
// global functions
void *thread_write(void *arg);
void *thread_read(void *arg);
//==============================================================================
// The main entry-point function.
int main(int argc, char **argv) {
// Initialize the rwlock
pthread_rwlock_init(&g_rw_data.rwlock, NULL);
// TODO: Create threads and use the rwlock
// Destroy the rwlock
pthread_rwlock_destroy(&g_rw_data.rwlock);
exit(0);
}
void *thread_write(void *arg) {
// TODO: Write lock and write unlock
return您的问题是在尝试使用读写锁(pthread_rw_lock_t)时遇到了“未知的类型名 pthread_rw_lock_t”的错误。这个问题的原因是,pthread库中并没有定义pthread_rw_lock_t这个类型。在POSIX线程库中,读写锁的类型是pthread_rwlock_t,而不是pthread_rw_lock_t。
所以,您需要将代码中的pthread_rw_lock_t更改为pthread_rwlock_t。同时,您需要使用正确的函数来初始化、读取锁定、写入锁定、解锁和销毁读写锁。这些函数分别是pthread_rwlock_init、pthread_rwlock_rdlock、pthread_rwlock_wrlock、pthread_rwlock_unlock和pthread_rwlock_destroy。
以下是您的代码的修正版本:
`c
/*
* @Description: multi-thread sync : read-write lock
* @Author:
* @version:
* @Date: 2023-10-16 10:01:24
* @LastEditors:
* @LastEditTime: 2023-10-16 10:31:11
*/
//==============================================================================
// Include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
//==============================================================================
// Constants
#define MAX_N_WRITE 3
#define MAX_N_READ 5
#define MAX(a,b) ((a)>(b) ? (a):(b))
#define MIN(a,b) ((a)<(b) ? (a):(b))
//==============================================================================
// types
struct RW_DATA_ST {
pthread_rwlock_t rwlock;
int number;
};
//==============================================================================
// global varibles
struct RW_DATA_ST g_rw_data;
//==============================================================================
// global functions
void *thread_write(void *arg);
void *thread_read(void *arg);
//==============================================================================
// The main entry-point function.
int main(int argc, char **argv) {
// Initialize the rwlock
pthread_rwlock_init(&g_rw_data.rwlock, NULL);
// TODO: Create threads and use the rwlock
// Destroy the rwlock
pthread_rwlock_destroy(&g_rw_data.rwlock);
exit(0);
}
void *thread_write(void *arg) {
// TODO: Write lock and write unlock
return (NULL);
}
void *thread_read(void *arg) {
// TODO: Read lock and read unlock
return (NULL);
}
请注意,我在main函数中添加了初始化和销毁读写锁的代码,但是还需要您自己创建线程并使用读写锁。在thread_write和thread_read函数中,您需要添加写锁定和解锁,以及读锁定和解锁的代码。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案 isdkz 发表于 2023-10-16 16:17
你好,这个错误是由于缺少pthread库导致的。你需要在编译时链接pthread库,可以在编译时加上-lpt ...
经过测试,还是不行。 陶远航 发表于 2023-10-16 16:18
您的问题是在尝试使用读写锁(pthread_rw_lock_t)时遇到了“未知的类型名 pthread_rw_lock_t”的错误。这 ...
好像编译通过了,GPT大法强啊
页:
[1]