鱼C论坛

 找回密码
 立即注册
查看: 1402|回复: 5

C语言线程和链表

[复制链接]
发表于 2021-12-28 10:12:41 | 显示全部楼层 |阅读模式

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

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

x
实现步骤:
1. 创建两个线程(线程A和线程B)
2. 实现链表的增删打印功能
3. 实现线程A监测用户输入。 输入data(不定长,涉及到malloc), 以及该data保存的时间time。并插入到链表。每添加一个节点就打印所有节点
4. 实现线程B定时监测链表的数据,当time到期之后就删除该data节点。每删除一个节点就打印所有节点
有大佬教教我怎么写吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-29 20:23:37 | 显示全部楼层
main.c
  1. #include "common.h"
  2. #include <stdlib.h>

  3. parameter_t *parameter_init(void) {
  4.     parameter_t *parameter = malloc(sizeof(*parameter));
  5.     parameter->exit = false;
  6.     parameter->list = list_init();
  7.     pthread_mutex_init(&parameter->mutex, NULL);
  8.     return parameter;
  9. }

  10. void parameter_deinit(parameter_t *parameter) {
  11.     pthread_mutex_destroy(&parameter->mutex);
  12.     list_deinit(parameter->list);
  13.     free(parameter);
  14. }

  15. int main(void) {
  16.     parameter_t *parameter = parameter_init();
  17.     pthread_t thread[2];
  18.     pthread_create(&thread[0], NULL, thread_A, parameter);
  19.     pthread_create(&thread[1], NULL, thread_B, parameter);
  20.     void *rv;
  21.     pthread_join(thread[0], &rv);
  22.     pthread_join(thread[1], &rv);
  23.     parameter_deinit(parameter);
  24.     return 0;
  25. }
复制代码


common.h
  1. #ifndef _COMMON_H_
  2. #define _COMMON_H_

  3. #include "list.h"
  4. #include <stdbool.h>
  5. #include <pthread.h>
  6. #include <time.h>

  7. typedef struct {
  8.     volatile bool exit;
  9.     list_t *list;
  10.     pthread_mutex_t mutex;
  11. } parameter_t;

  12. typedef struct {
  13.     size_t data;
  14.     time_t time;
  15. } element_t;

  16. void *thread_A();
  17. void *thread_B();
  18. void list_output(const list_t *list);

  19. #endif
复制代码


common.c
  1. #include "common.h"
  2. #include <stdio.h>

  3. void list_output(const list_t *list) {
  4.     for(size_t i = 0; i < list_size(list); ++i) {
  5.         element_t e;
  6.         list_get(list, i, &e, sizeof(e));
  7.         printf("%lu ", e.data);
  8.     }
  9.     puts("");
  10. }
复制代码


list.h
  1. #ifndef _LIST_H_
  2. #define _LIST_H_

  3. #include <stddef.h>
  4. #include <stdbool.h>

  5. struct list_node_tag {
  6.     void *data; size_t size;
  7.     struct list_node_tag *next;
  8. };

  9. typedef struct {
  10.     struct list_node_tag *head;
  11.     size_t size;
  12. } list_t;

  13. list_t *list_init(void);
  14. void list_deinit(list_t *list);
  15. bool list_clean(list_t *list);
  16. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  17. bool list_delete(list_t *list, size_t index);
  18. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  19. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  20. bool list_append(list_t *list, const void *data, size_t size);
  21. size_t list_size(const list_t *list);
  22. bool list_empty(const list_t *list);

  23. #endif
复制代码


list.c
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <memory.h>

  4. list_t *list_init(void) {
  5.     list_t *list = malloc(sizeof(*list));
  6.     if(!list) return NULL;
  7.     list->head = NULL;
  8.     list->size = 0;
  9.     return list;
  10. }

  11. void list_deinit(list_t *list) {
  12.     if(!list) return;
  13.     list_clean(list);
  14.     free(list);
  15. }

  16. bool list_clean(list_t *list) {
  17.     if(!list) return false;
  18.     while(!list_empty(list)) list_delete(list, 0);
  19.     return true;
  20. }

  21. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  22.     if(!list) return false;
  23.     if(list_size(list) < index) return false;
  24.     if(!data) return false;
  25.     struct list_node_tag **current = &list->head;
  26.     while(index--) current = &(*current)->next;
  27.     struct list_node_tag *node = malloc(sizeof(*node));
  28.     if(!node) return false;
  29.     node->data = malloc(size);
  30.     if(!node->data) {free(node); return false;}
  31.     memcpy(node->data, data, size);
  32.     node->size = size;
  33.     node->next = *current;
  34.     *current = node;
  35.     ++list->size;
  36.     return true;
  37. }

  38. bool list_delete(list_t *list, size_t index) {
  39.     if(!list) return false;
  40.     if(list_size(list) <= index) return false;
  41.     struct list_node_tag **current = &list->head;
  42.     while(index--) current = &(*current)->next;
  43.     struct list_node_tag *temp = *current;
  44.     *current = temp->next;
  45.     free(temp->data); free(temp);
  46.     --list->size;
  47.     return true;
  48. }

  49. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  50.     if(!list) return false;
  51.     if(list_size(list) <= index) return false;
  52.     if(!data) return false;
  53.     struct list_node_tag *const *current = &list->head;
  54.     while(index--) current = &(*current)->next;
  55.     struct list_node_tag *temp = *current;
  56.     if(temp->size > size) return false;
  57.     memcpy(data, temp->data, temp->size);
  58.     return true;
  59. }

  60. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  61.     bool res = list_delete(list, index);
  62.     return res ? list_insert(list, index, data, size) : res;
  63. }

  64. bool list_append(list_t *list, const void *data, size_t size) {
  65.     if(!list) return false;
  66.     return list_insert(list, list_size(list), data, size);
  67. }

  68. size_t list_size(const list_t *list) {
  69.     if(!list) return 0;
  70.     return list->size;
  71. }

  72. bool list_empty(const list_t *list) {
  73.     if(!list) return true;
  74.     return list_size(list) == 0;
  75. }
复制代码


thread_A.c
  1. #include "common.h"
  2. #include <stdio.h>

  3. void *thread_A(parameter_t *parameter) {
  4.     while(1) {
  5.         element_t e;
  6.         if(scanf("%lu%lu", &e.data, &e.time) != 2) break;
  7.         if(!e.time) break;
  8.         pthread_mutex_lock(&parameter->mutex);
  9.         list_append(parameter->list, &e, sizeof(e));
  10.         list_output(parameter->list);
  11.         pthread_mutex_unlock(&parameter->mutex);
  12.     }
  13.     parameter->exit = true;
  14.     return NULL;
  15. }
复制代码


thread_B.c
  1. #include "common.h"
  2. #include <unistd.h>

  3. static void list_data_next(list_t *list) {
  4.     for(size_t i = 0; i < list_size(list); ++i) {
  5.         element_t e;
  6.         list_get(list, i, &e, sizeof(e));
  7.         if(!--e.time) {
  8.             list_delete(list, i);
  9.             list_output(list);
  10.             continue;
  11.         }
  12.         list_set(list, i, &e, sizeof(e));
  13.     }
  14. }

  15. void *thread_B(parameter_t *parameter) {
  16.     static size_t count;
  17.     while(1) {
  18.         usleep(100000);
  19.         if(parameter->exit) break;
  20.         if(++count == 10) {
  21.             count = 0;
  22.             pthread_mutex_lock(&parameter->mutex);
  23.             list_data_next(parameter->list);
  24.             pthread_mutex_unlock(&parameter->mutex);
  25.         }
  26.     }
  27.     return NULL;
  28. }
复制代码

  1. gcc -g -Wall -o main main.c list.c thread_A.c thread_B.c common.c -lpthread
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
傻眼貓咪 + 5 + 5 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-12-30 16:31:34 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-30 16:39:07 | 显示全部楼层

pthread_create是不是不能直接用于window,window是不是得用CreateThread
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-30 17:15:06 | 显示全部楼层
jhq999 发表于 2021-12-30 16:39
pthread_create是不是不能直接用于window,window是不是得用CreateThread

windows下可以装一个cygwin来用这个代码
我记得好像dev-cpp也能用这个代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-30 18:42:01 | 显示全部楼层
人造人 发表于 2021-12-30 17:15
windows下可以装一个cygwin来用这个代码
我记得好像dev-cpp也能用这个代码

谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-21 18:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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