资源简介
C实现的多线程(pthread)安全链表数据结构
包括member, insert, delete, traverse基本操作
编译时需要链接pthread库,如
gcc -O3 SortList2.c -lpthread
代码片段和文件信息
/*
============================================================================
Name : SortList.c
Author : Elvis Hao
Version :
Copyright : Your copyright notice
Description : Hello World in C Ansi-style
============================================================================
*/
#include
#include
#include
#include
#define LIST_SIZE 1000
#define RUN_TIMES 10
#define OP_TIMES 100000
#define THREAD_NUM 8
struct list_node_s {
int data;
struct list_node_s* next;
pthread_mutex_t mutex;
};
typedef struct list_node_s Sort_List_Node;
Sort_List_Node *head_p;
int member(int value Sort_List_Node* head_p) {
Sort_List_Node* curr_node;
if (head_p == NULL) {
return 0;
}
curr_node = head_p -> next;
pthread_mutex_lock(&(curr_node->mutex));
while (curr_node != NULL && value > curr_node -> data) {
pthread_mutex_unlock(&(curr_node->mutex));
curr_node = curr_node -> next;
if (curr_node != NULL) {
pthread_mutex_lock(&(curr_node->mutex));
}
}
if (curr_node != NULL && value == curr_node -> data) {
pthread_mutex_unlock(&(curr_node->mutex));
return 1;
}
return 0;
}
int insert(int value Sort_List_Node* head_p) {
Sort_List_Node *new_node_p *pre_temp*temp;
new_node_p = (Sort_List_Node *)malloc(sizeof(Sort_List_Node));
new_node_p -> data = value;
new_node_p -> next = NULL;
pthread_mutex_init(&(new_node_p -> mutex) NULL);
pthread_mutex_lock(&(head_p -> mutex));
if (head_p -> next ==NULL) {
head_p -> next = new_node_p;
pthread_mutex_unlock(&(head_p -> mutex));
return 1;
}
pthread_mutex_lock(&((head_p -> next) -> mutex));
pre_temp = head_p;
temp = head_p -> next;
while (temp != NULL && temp -> data <= value) {
if (temp -> next != NULL) {
pthread_mutex_lock(&((temp -> next)->mutex));
}
pthread_mutex_unlock(&(pre_temp -> mutex));
pre_temp = pre_temp -> next;
temp = temp -> next;
}
new_node_p -> next = temp;
pre_temp -> next = new_node_p;
if (temp != NULL) {
pthread_mutex_unlock(&(temp -> mutex));
}
pthread_mutex_unlock(&(pre_temp -> mutex));
return 1;
}
int delete(int value Sort_List_Node *head_p) {
Sort_List_Node *pre_temp *temp;
pthread_mutex_lock(&(head_p -> mutex));
pre_temp = head_p;
temp = pre_temp -> next;
if (temp == NULL) {
pthread_mutex_unlock(&(head_p -> mutex));
return 0;
}
pthread_mutex_lock(&(temp -> mutex));
while (temp != NULL && temp -> data < value) {
if (temp -> next != NULL) {
pthread_mutex_lock(&(temp -> next -> mutex));
}
pthread_mutex_unlock(&(pre_temp -> mutex));
pre_temp = pre_temp -> next;
temp = temp -> next;
}
if (temp == NULL) {
pthread_mutex_unlock(&(pre_temp -> mutex));
return 0;
} else if (temp -> data == value) {
pre_temp -> next = temp -> next;
pthread_mutex_unlock(&(temp -> mutex));
free(temp);
pthread_mutex_unlock(&(pre_temp -> mutex));
return 1;
} else {
pthread_mutex_unlock(&(temp -> mutex));
pthread_mutex_unlock(&(pre_temp -> mutex));
ret
- 上一篇:boost asio 服务器和客户端 TCP
- 下一篇:ios仿淘宝购物车demo
相关资源
- boost asio 服务器和客户端 TCP
- 使用DAC0832的DA转换实验Proteus8086
- Socket实战
- dedecms v57网银在线支付接口含UTF8和G
- ubuntu和centos通用的smb.conf文件
- 四字节数转换BCD码
- Memcached相关程序
- 数码管显示PCF8563实时时钟日历
- 尚硅谷2018大数据全套
- STM32无刷直流电机PWM控制
- 武汉理工大学计算机基础综合实验
- 利用TEQC的格式转换、数据编辑、质量
- EP1C3T144C8原理图.pdf
- SLEP(Sparse Learning with Efficient Projecti
- Arduino HMC5883L库文件
- Socket 类封装 改进版
- UniPatcher_v2017.4.exe
- verilog 16进制转10进制bcd码
- 一个真正的所见即所得的PB导出EXCEL
- 中文翻译Mastering Chess and Shogi by Self_
- Mastering Chess and Shogi by Self_Play with a
- WinCC-Graphics.zip
- IET control theory and application 期刊的最新
- 二维多边形布尔运算,包括多边形绘
- GDI+绘图功能软件
- 2015年最新版帝国cms百度ping插件V3.0
- 计算文件哈希值的程序
- cc2530模块底板电路图
- MODBUS-CSharp tcp测试正常
- C620普遍车床的数控化改造(本科)
评论
共有 条评论