91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 6KB
    文件類型: .c
    金幣: 1
    下載: 0 次
    發布日期: 2021-06-11
  • 語言: 其他
  • 標簽: C??多線程??鏈表??

資源簡介

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? 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

評論

共有 條評論