C语言线程和链表
实现步骤:1. 创建两个线程(线程A和线程B)
2. 实现链表的增删打印功能
3. 实现线程A监测用户输入。 输入data(不定长,涉及到malloc), 以及该data保存的时间time。并插入到链表。每添加一个节点就打印所有节点
4. 实现线程B定时监测链表的数据,当time到期之后就删除该data节点。每删除一个节点就打印所有节点
有大佬教教我怎么写吗 main.c
#include "common.h"
#include <stdlib.h>
parameter_t *parameter_init(void) {
parameter_t *parameter = malloc(sizeof(*parameter));
parameter->exit = false;
parameter->list = list_init();
pthread_mutex_init(¶meter->mutex, NULL);
return parameter;
}
void parameter_deinit(parameter_t *parameter) {
pthread_mutex_destroy(¶meter->mutex);
list_deinit(parameter->list);
free(parameter);
}
int main(void) {
parameter_t *parameter = parameter_init();
pthread_t thread;
pthread_create(&thread, NULL, thread_A, parameter);
pthread_create(&thread, NULL, thread_B, parameter);
void *rv;
pthread_join(thread, &rv);
pthread_join(thread, &rv);
parameter_deinit(parameter);
return 0;
}
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include "list.h"
#include <stdbool.h>
#include <pthread.h>
#include <time.h>
typedef struct {
volatile bool exit;
list_t *list;
pthread_mutex_t mutex;
} parameter_t;
typedef struct {
size_t data;
time_t time;
} element_t;
void *thread_A();
void *thread_B();
void list_output(const list_t *list);
#endif
common.c
#include "common.h"
#include <stdio.h>
void list_output(const list_t *list) {
for(size_t i = 0; i < list_size(list); ++i) {
element_t e;
list_get(list, i, &e, sizeof(e));
printf("%lu ", e.data);
}
puts("");
}
list.h
#ifndef _LIST_H_
#define _LIST_H_
#include <stddef.h>
#include <stdbool.h>
struct list_node_tag {
void *data; size_t size;
struct list_node_tag *next;
};
typedef struct {
struct list_node_tag *head;
size_t size;
} list_t;
list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_set(list_t *list, size_t index, const void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);
#endif
list.c
#include "list.h"
#include <stdlib.h>
#include <memory.h>
list_t *list_init(void) {
list_t *list = malloc(sizeof(*list));
if(!list) return NULL;
list->head = NULL;
list->size = 0;
return list;
}
void list_deinit(list_t *list) {
if(!list) return;
list_clean(list);
free(list);
}
bool list_clean(list_t *list) {
if(!list) return false;
while(!list_empty(list)) list_delete(list, 0);
return true;
}
bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
if(!list) return false;
if(list_size(list) < index) return false;
if(!data) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *node = malloc(sizeof(*node));
if(!node) return false;
node->data = malloc(size);
if(!node->data) {free(node); return false;}
memcpy(node->data, data, size);
node->size = size;
node->next = *current;
*current = node;
++list->size;
return true;
}
bool list_delete(list_t *list, size_t index) {
if(!list) return false;
if(list_size(list) <= index) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
*current = temp->next;
free(temp->data); free(temp);
--list->size;
return true;
}
bool list_get(const list_t *list, size_t index, void *data, size_t size) {
if(!list) return false;
if(list_size(list) <= index) return false;
if(!data) return false;
struct list_node_tag *const *current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
if(temp->size > size) return false;
memcpy(data, temp->data, temp->size);
return true;
}
bool list_set(list_t *list, size_t index, const void *data, size_t size) {
bool res = list_delete(list, index);
return res ? list_insert(list, index, data, size) : res;
}
bool list_append(list_t *list, const void *data, size_t size) {
if(!list) return false;
return list_insert(list, list_size(list), data, size);
}
size_t list_size(const list_t *list) {
if(!list) return 0;
return list->size;
}
bool list_empty(const list_t *list) {
if(!list) return true;
return list_size(list) == 0;
}
thread_A.c
#include "common.h"
#include <stdio.h>
void *thread_A(parameter_t *parameter) {
while(1) {
element_t e;
if(scanf("%lu%lu", &e.data, &e.time) != 2) break;
if(!e.time) break;
pthread_mutex_lock(¶meter->mutex);
list_append(parameter->list, &e, sizeof(e));
list_output(parameter->list);
pthread_mutex_unlock(¶meter->mutex);
}
parameter->exit = true;
return NULL;
}
thread_B.c
#include "common.h"
#include <unistd.h>
static void list_data_next(list_t *list) {
for(size_t i = 0; i < list_size(list); ++i) {
element_t e;
list_get(list, i, &e, sizeof(e));
if(!--e.time) {
list_delete(list, i);
list_output(list);
continue;
}
list_set(list, i, &e, sizeof(e));
}
}
void *thread_B(parameter_t *parameter) {
static size_t count;
while(1) {
usleep(100000);
if(parameter->exit) break;
if(++count == 10) {
count = 0;
pthread_mutex_lock(¶meter->mutex);
list_data_next(parameter->list);
pthread_mutex_unlock(¶meter->mutex);
}
}
return NULL;
}
gcc -g -Wall -o main main.c list.c thread_A.c thread_B.c common.c -lpthread 人造人 发表于 2021-12-29 20:23
main.c
{:5_106:} 人造人 发表于 2021-12-29 20:23
main.c
pthread_create是不是不能直接用于window,window是不是得用CreateThread jhq999 发表于 2021-12-30 16:39
pthread_create是不是不能直接用于window,window是不是得用CreateThread
windows下可以装一个cygwin来用这个代码
我记得好像dev-cpp也能用这个代码
人造人 发表于 2021-12-30 17:15
windows下可以装一个cygwin来用这个代码
我记得好像dev-cpp也能用这个代码
谢谢
页:
[1]