鱼C论坛

 找回密码
 立即注册
查看: 1719|回复: 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

  1. /*
  2. * @Description: multi-thread sync : read-write lock
  3. * @Author:
  4. * @version:
  5. * @Date: 2023-10-16 10:01:24
  6. * @LastEditors:
  7. * @LastEditTime: 2023-10-16 10:31:11
  8. */

  9. //==============================================================================
  10. // Include files
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <unistd.h>
  16. #include <pthread.h>

  17. //==============================================================================
  18. // Constants


  19. #define MAX_N_WRITE 3
  20. #define MAX_N_READ 5

  21. #define MAX(a,b) ((a)>(b) ? (a):(b))
  22. #define MIN(a,b) ((a)<(b) ? (a):(b))
  23. //==============================================================================
  24. // types

  25. struct RW_DATA_ST
  26. {
  27.     pthread_rw_lock_t rwlock;
  28.     int number;
  29. };

  30. //==============================================================================
  31. // global varibles

  32. struct RW_DATA_ST g_rw_data;

  33. //==============================================================================
  34. // global functions

  35. void *thread_write(void *arg);
  36. void *thread_read(void *arg);

  37. //==============================================================================
  38. // The main entry-point function.

  39. int main(int argc, char **argv)
  40. {
  41.    

  42.     exit(0);
  43. }

  44. void *thread_write(void *arg)
  45. {
  46.     return (NULL);
  47. }

  48. void *thread_read(void *arg)
  49. {
  50.     return (NULL);
  51. }
复制代码





最佳答案
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。

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

  1. /*  
  2. * @Description: multi-thread sync : read-write lock  
  3. * @Author:  
  4. * @version:   
  5. * @Date: 2023-10-16 10:01:24  
  6. * @LastEditors:   
  7. * @LastEditTime: 2023-10-16 10:31:11  
  8. */  

  9. //==============================================================================
  10. // Include files

  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <unistd.h>
  16. #include <pthread.h>  

  17. //==============================================================================
  18. // Constants   

  19. #define MAX_N_WRITE 3
  20. #define MAX_N_READ 5  

  21. #define MAX(a,b) ((a)>(b) ? (a):(b))  

  22. #define MIN(a,b) ((a)<(b) ? (a):(b))

  23. //==============================================================================
  24. // types  

  25. struct RW_DATA_ST {     
  26.     pthread_rwlock_t rwlock;     
  27.     int number;
  28. };  

  29. //==============================================================================
  30. // global varibles  

  31. struct RW_DATA_ST g_rw_data;  

  32. //==============================================================================
  33. // global functions  

  34. void *thread_write(void *arg);
  35. void *thread_read(void *arg);  

  36. //==============================================================================
  37. // The main entry-point function.  

  38. int main(int argc, char **argv) {   
  39.     // Initialize the rwlock
  40.     pthread_rwlock_init(&g_rw_data.rwlock, NULL);

  41.     // TODO: Create threads and use the rwlock

  42.     // Destroy the rwlock
  43.     pthread_rwlock_destroy(&g_rw_data.rwlock);

  44.     exit(0);
  45. }  

  46. void *thread_write(void *arg) {     
  47.     // TODO: Write lock and write unlock
  48.     return您的问题是在尝试使用读写锁(pthread_rw_lock_t)时遇到了“未知的类型名 pthread_rw_lock_t”的错误。这个问题的原因是,pthread库中并没有定义pthread_rw_lock_t这个类型。在POSIX线程库中,读写锁的类型是pthread_rwlock_t,而不是pthread_rw_lock_t。

  49. 所以,您需要将代码中的pthread_rw_lock_t更改为pthread_rwlock_t。同时,您需要使用正确的函数来初始化、读取锁定、写入锁定、解锁和销毁读写锁。这些函数分别是pthread_rwlock_init、pthread_rwlock_rdlock、pthread_rwlock_wrlock、pthread_rwlock_unlock和pthread_rwlock_destroy。

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

  51. [b][/b]`c
  52. /*  
  53. * @Description: multi-thread sync : read-write lock  
  54. * @Author:  
  55. * @version:   
  56. * @Date: 2023-10-16 10:01:24  
  57. * @LastEditors:   
  58. * @LastEditTime: 2023-10-16 10:31:11  
  59. */  

  60. //==============================================================================
  61. // Include files

  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <sys/types.h>
  66. #include <unistd.h>
  67. #include <pthread.h>  

  68. //==============================================================================
  69. // Constants   

  70. #define MAX_N_WRITE 3
  71. #define MAX_N_READ 5  

  72. #define MAX(a,b) ((a)>(b) ? (a):(b))  

  73. #define MIN(a,b) ((a)<(b) ? (a):(b))

  74. //==============================================================================
  75. // types  

  76. struct RW_DATA_ST {     
  77.     pthread_rwlock_t rwlock;     
  78.     int number;
  79. };  

  80. //==============================================================================
  81. // global varibles  

  82. struct RW_DATA_ST g_rw_data;  

  83. //==============================================================================
  84. // global functions  

  85. void *thread_write(void *arg);
  86. void *thread_read(void *arg);  

  87. //==============================================================================
  88. // The main entry-point function.  

  89. int main(int argc, char **argv) {   
  90.     // Initialize the rwlock
  91.     pthread_rwlock_init(&g_rw_data.rwlock, NULL);

  92.     // TODO: Create threads and use the rwlock

  93.     // Destroy the rwlock
  94.     pthread_rwlock_destroy(&g_rw_data.rwlock);

  95.     exit(0);
  96. }  

  97. void *thread_write(void *arg) {     
  98.     // TODO: Write lock and write unlock
  99.     return (NULL);
  100. }  

  101. void *thread_read(void *arg) {     
  102.     // TODO: Read lock and read unlock
  103.     return (NULL);
  104. }
复制代码


请注意,我在main函数中添加了初始化和销毁读写锁的代码,但是还需要您自己创建线程并使用读写锁。在thread_write和thread_read函数中,您需要添加写锁定和解锁,以及读锁定和解锁的代码。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
2023-10-16_16-11-08.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

回帖奖励 +20 鱼币

你好,这个错误是由于缺少  pthread  库导致的。你需要在编译时链接  pthread  库,可以在编译时加上  -lpthread  参数来链接该库。修改代码如下:

  1. /*
  2. * @Description: multi-thread sync : read-write lock
  3. * @Author:
  4. * @version:
  5. * @Date: 2023-10-16 10:01:24
  6. * @LastEditors:
  7. * @LastEditTime: 2023-10-16 10:31:11
  8. */

  9. //==============================================================================
  10. // Include files
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <unistd.h>
  16. #include <pthread.h>

  17. //==============================================================================
  18. // Constants

  19. #define MAX_N_WRITE 3
  20. #define MAX_N_READ 5

  21. #define MAX(a,b) ((a)>(b) ? (a):(b))
  22. #define MIN(a,b) ((a)<(b) ? (a):(b))
  23. //==============================================================================
  24. // types

  25. struct RW_DATA_ST
  26. {
  27.     pthread_rwlock_t rwlock;  // 修改此处
  28.     int number;
  29. };

  30. //==============================================================================
  31. // global varibles

  32. struct RW_DATA_ST g_rw_data;

  33. //==============================================================================
  34. // global functions

  35. void *thread_write(void *arg);
  36. void *thread_read(void *arg);

  37. //==============================================================================
  38. // The main entry-point function.

  39. int main(int argc, char **argv)
  40. {
  41.     pthread_rwlock_init(&g_rw_data.rwlock, NULL);  // 初始化读写锁

  42.     pthread_t write_tid[MAX_N_WRITE];
  43.     pthread_t read_tid[MAX_N_READ];

  44.     int i = 0;
  45.     for (i = 0; i < MAX_N_WRITE; i++)
  46.     {
  47.         pthread_create(&write_tid[ i], NULL, thread_write, (void*)i);
  48.     }

  49.     for (i = 0; i < MAX_N_READ; i++)
  50.     {
  51.         pthread_create(&read_tid[ i], NULL, thread_read, (void*)i);
  52.     }

  53.     for (i = 0; i < MAX_N_WRITE; i++)
  54.     {
  55.         pthread_join(write_tid[ i], NULL);
  56.     }

  57.     for (i = 0; i < MAX_N_READ; i++)
  58.     {
  59.         pthread_join(read_tid[ i], NULL);
  60.     }

  61.     pthread_rwlock_destroy(&g_rw_data.rwlock);  // 销毁读写锁

  62.     exit(0);
  63. }

  64. void *thread_write(void *arg)
  65. {
  66.     int i = 0;
  67.     for (i = 0; i < 10; i++)
  68.     {
  69.         pthread_rwlock_wrlock(&g_rw_data.rwlock);  // 获取写锁
  70.         g_rw_data.number = i;
  71.         printf("write thread %d write number %d\n", (int)arg, g_rw_data.number);
  72.         pthread_rwlock_unlock(&g_rw_data.rwlock);  // 释放写锁
  73.         sleep(1);
  74.     }
  75.     return (NULL);
  76. }

  77. void *thread_read(void *arg)
  78. {
  79.     int i = 0;
  80.     for (i = 0; i < 10; i++)
  81.     {
  82.         pthread_rwlock_rdlock(&g_rw_data.rwlock);  // 获取读锁
  83.         printf("read thread %d read number %d\n", (int)arg, g_rw_data.number);
  84.         pthread_rwlock_unlock(&g_rw_data.rwlock);  // 释放读锁
  85.         sleep(1);
  86.     }
  87.     return (NULL);
  88. }
复制代码


编译时使用以下命令:

  1. gcc -o test test.c -lpthread
复制代码


注意:请根据实际需求修改代码中的读写锁的初始化和销毁位置。
小甲鱼最新课程 -> https://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。

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

  1. /*  
  2. * @Description: multi-thread sync : read-write lock  
  3. * @Author:  
  4. * @version:   
  5. * @Date: 2023-10-16 10:01:24  
  6. * @LastEditors:   
  7. * @LastEditTime: 2023-10-16 10:31:11  
  8. */  

  9. //==============================================================================
  10. // Include files

  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <unistd.h>
  16. #include <pthread.h>  

  17. //==============================================================================
  18. // Constants   

  19. #define MAX_N_WRITE 3
  20. #define MAX_N_READ 5  

  21. #define MAX(a,b) ((a)>(b) ? (a):(b))  

  22. #define MIN(a,b) ((a)<(b) ? (a):(b))

  23. //==============================================================================
  24. // types  

  25. struct RW_DATA_ST {     
  26.     pthread_rwlock_t rwlock;     
  27.     int number;
  28. };  

  29. //==============================================================================
  30. // global varibles  

  31. struct RW_DATA_ST g_rw_data;  

  32. //==============================================================================
  33. // global functions  

  34. void *thread_write(void *arg);
  35. void *thread_read(void *arg);  

  36. //==============================================================================
  37. // The main entry-point function.  

  38. int main(int argc, char **argv) {   
  39.     // Initialize the rwlock
  40.     pthread_rwlock_init(&g_rw_data.rwlock, NULL);

  41.     // TODO: Create threads and use the rwlock

  42.     // Destroy the rwlock
  43.     pthread_rwlock_destroy(&g_rw_data.rwlock);

  44.     exit(0);
  45. }  

  46. void *thread_write(void *arg) {     
  47.     // TODO: Write lock and write unlock
  48.     return您的问题是在尝试使用读写锁(pthread_rw_lock_t)时遇到了“未知的类型名 pthread_rw_lock_t”的错误。这个问题的原因是,pthread库中并没有定义pthread_rw_lock_t这个类型。在POSIX线程库中,读写锁的类型是pthread_rwlock_t,而不是pthread_rw_lock_t。

  49. 所以,您需要将代码中的pthread_rw_lock_t更改为pthread_rwlock_t。同时,您需要使用正确的函数来初始化、读取锁定、写入锁定、解锁和销毁读写锁。这些函数分别是pthread_rwlock_init、pthread_rwlock_rdlock、pthread_rwlock_wrlock、pthread_rwlock_unlock和pthread_rwlock_destroy。

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

  51. [b][/b]`c
  52. /*  
  53. * @Description: multi-thread sync : read-write lock  
  54. * @Author:  
  55. * @version:   
  56. * @Date: 2023-10-16 10:01:24  
  57. * @LastEditors:   
  58. * @LastEditTime: 2023-10-16 10:31:11  
  59. */  

  60. //==============================================================================
  61. // Include files

  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <sys/types.h>
  66. #include <unistd.h>
  67. #include <pthread.h>  

  68. //==============================================================================
  69. // Constants   

  70. #define MAX_N_WRITE 3
  71. #define MAX_N_READ 5  

  72. #define MAX(a,b) ((a)>(b) ? (a):(b))  

  73. #define MIN(a,b) ((a)<(b) ? (a):(b))

  74. //==============================================================================
  75. // types  

  76. struct RW_DATA_ST {     
  77.     pthread_rwlock_t rwlock;     
  78.     int number;
  79. };  

  80. //==============================================================================
  81. // global varibles  

  82. struct RW_DATA_ST g_rw_data;  

  83. //==============================================================================
  84. // global functions  

  85. void *thread_write(void *arg);
  86. void *thread_read(void *arg);  

  87. //==============================================================================
  88. // The main entry-point function.  

  89. int main(int argc, char **argv) {   
  90.     // Initialize the rwlock
  91.     pthread_rwlock_init(&g_rw_data.rwlock, NULL);

  92.     // TODO: Create threads and use the rwlock

  93.     // Destroy the rwlock
  94.     pthread_rwlock_destroy(&g_rw_data.rwlock);

  95.     exit(0);
  96. }  

  97. void *thread_write(void *arg) {     
  98.     // TODO: Write lock and write unlock
  99.     return (NULL);
  100. }  

  101. void *thread_read(void *arg) {     
  102.     // TODO: Read lock and read unlock
  103.     return (NULL);
  104. }
复制代码


请注意,我在main函数中添加了初始化和销毁读写锁的代码,但是还需要您自己创建线程并使用读写锁。在thread_write和thread_read函数中,您需要添加写锁定和解锁,以及读锁定和解锁的代码。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

经过测试,还是不行。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

好像编译通过了,GPT大法强啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 12:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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