资源简介
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
相关资源
- Power Electronic Converters Modeling and Contr
- CE修改器风叶人加强版
- Oracle+11gR2+RAC安装详解(HP-UX+11.31+ia6
- Oracle+GoldenGate运维完全手册
- STM32F103RC+ADC+DMA多通道采样LCD显示
- Modbus协议官方文档中、英文全
- pcbtemp电流计算软件
- I2C读写AT24C02 基于STM32F103 cube116540
- Scratch源码
- Microsoft Forms 2.0107770
- 实验三 消息中间件应用开发:Active
- WCE注入工具
- ModelGoon-4.4.1-site.zip
- AsyncTask文件控制暂停和继续,在状态
- Visio大全模具(含Cisco、IBM等常用拓扑
- 信号奇异点Lipschitz指数计算
- 基于STM32RCT6的步进电机驱动程序
- 酒店管理系统基于Qt Creator5)
- 用友NC开发API字典
- Navicat Premium 15汉化包.zip55438
- 登录注册界面.zip48872
- 条码字体barcode128
- Rational Rose Common破解文件
- res10_300x300_ssd_iter_140000.caffemodel与dep
- scratch 第1课 翻跟斗的小猫(入门)
- stm32f407上的两个can发送和接收例程
- Scrach 欢乐狙击手.sb2
- 04741计算机网络原理知识点整理.docx(
- Wolfram Mathematica 矩阵初等变换函数(
- pscad近海风电模型 Fortran语言
川公网安备 51152502000135号
评论
共有 条评论