鱼C论坛

 找回密码
 立即注册
查看: 742|回复: 4

[已解决]Error: 未知的类型名 pthread_rw_lock_t

[复制链接]
发表于 2023-10-16 16:16:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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);
}




最佳答案
2023-10-16 16:18:16
您的问题是在尝试使用读写锁(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。

以下是您的代码的修正版本:

[b][/b]`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
如果问题已经解决,请设置最佳答案
2023-10-16_16-11-08.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-16 16:17:10 | 显示全部楼层

回帖奖励 +20 鱼币

你好,这个错误是由于缺少  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[MAX_N_WRITE];
    pthread_t read_tid[MAX_N_READ];

    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

注意:请根据实际需求修改代码中的读写锁的初始化和销毁位置。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-16 16:18:16 | 显示全部楼层    本楼为最佳答案   
您的问题是在尝试使用读写锁(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。

以下是您的代码的修正版本:

[b][/b]`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
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-16 16:19:31 | 显示全部楼层
isdkz 发表于 2023-10-16 16:17
你好,这个错误是由于缺少  pthread  库导致的。你需要在编译时链接  pthread  库,可以在编译时加上  -lpt ...

经过测试,还是不行。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-16 17:24:25 | 显示全部楼层
陶远航 发表于 2023-10-16 16:18
您的问题是在尝试使用读写锁(pthread_rw_lock_t)时遇到了“未知的类型名 pthread_rw_lock_t”的错误。这 ...

好像编译通过了,GPT大法强啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 21:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表