C鏈表

  • 互斥鎖:鏈表用在多線程中保證順序,多個線程會操作同一個鏈表,互斥鎖保證多線程操作的安全,互斥鎖分情況使用,鏈表并不一定需要互斥鎖。
typedef struct SDL_mutex {
    pthread_mutex_t id;
} SDL_mutex;

typedef struct SDL_cond {
    pthread_cond_t id;
} SDL_cond;
  • 互斥鎖的操作
SDL_mutex *SDL_CreateMutex(void)
{
    SDL_mutex *mutex;
    mutex = (SDL_mutex *) mallocz(sizeof(SDL_mutex));
    if (!mutex)
        return NULL;

    if (pthread_mutex_init(&mutex->id, NULL) != 0) {
        free(mutex);
        return NULL;
    }

    return mutex;
}

void SDL_DestroyMutex(SDL_mutex *mutex)
{
    if (mutex) {
        pthread_mutex_destroy(&mutex->id);
        free(mutex);
    }
}

void SDL_DestroyMutexP(SDL_mutex **mutex)
{
    if (mutex) {
        SDL_DestroyMutex(*mutex);
        *mutex = NULL;
    }
}

int SDL_LockMutex(SDL_mutex *mutex)
{
    assert(mutex);
    if (!mutex)
        return -1;

    return pthread_mutex_lock(&mutex->id);
}

int SDL_UnlockMutex(SDL_mutex *mutex)
{
    assert(mutex);
    if (!mutex)
        return -1;

    return pthread_mutex_unlock(&mutex->id);
}

SDL_cond *SDL_CreateCond(void)
{
    SDL_cond *cond;
    cond = (SDL_cond *) mallocz(sizeof(SDL_cond));
    if (!cond)
        return NULL;

    if (pthread_cond_init(&cond->id, NULL) != 0) {
        free(cond);
        return NULL;
    }

    return cond;
}

void SDL_DestroyCond(SDL_cond *cond)
{
    if (cond) {
        pthread_cond_destroy(&cond->id);
        free(cond);
    }
}

void SDL_DestroyCondP(SDL_cond **cond)
{

    if (cond) {
        SDL_DestroyCond(*cond);
        *cond = NULL;
    }
}

int SDL_CondSignal(SDL_cond *cond)
{
    assert(cond);
    if (!cond)
        return -1;

    return pthread_cond_signal(&cond->id);
}

  • 鏈表和節點的定義聲明
typedef struct Queue{
    node *phead, *first_node, *last_node;
    int count;
    SDL_mutex *mutex;
    SDL_mutex *ex_mutex;
    SDL_cond *cond;
}queue;

typedef struct queue_node {
    struct queue_node* prev;
    struct queue_node* next;
    void *para;
} node;
  • 鏈表的邏輯結構
/**
雙向鏈表 創建一個節點

 @param para 要保存到鏈表的內容
 @return 返回創建的這個節點
 */
static node* create_node(void *para)
{
    node *pnode = NULL;
    pnode = (node *) malloc(sizeof(node));
    if (pnode)
    {
        pnode->prev = pnode->next = pnode;
        pnode->para = para;
    }
    return pnode;
}

/**
 創建鏈表的phead

 @param q 鏈表
 @return success return 0, fail return -1
 */
int create_queue(queue *q)
{
    q->phead = create_node(NULL);
    if (!q->phead)
        return -1;
    return 0;
}

/**
 初始化一個鏈表

 @param q 鏈表
 @return 鏈表的頭
 */
static int queue_init(queue *q)
{
    memset(q, 0, sizeof(queue));
    
    q->phead = NULL;
    q->last_node = NULL;
    q->first_node = NULL;
    q->count = 0;
    q->mutex = SDL_CreateMutex();
    if (!q->mutex)
    {
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    q->ex_mutex = SDL_CreateMutex();
    if (!q->ex_mutex)
    {
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    q->cond = SDL_CreateCond();
    if (!q->cond)
    {
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateCond(): %s\n", SDL_GetError());
        return AVERROR(ENOMEM);
    }
    return create_queue(q);
}

/**
 判斷鏈表是否為空

 @param q 鏈表
 @return 空:return 1,不是空:return 0
 */
int queue_is_empty(queue *q)
{
    return q->count == 0;
}

/**
 獲取鏈表長度

 @param q 鏈表
 @return 鏈表中元素的個數
 */
int queue_size(queue *q)
{
    return q->count;
}

/**
 根據index獲取鏈表的節點

 @param q 鏈表
 @param index 鏈表元素下標
 @return 該下標的鏈表元素節點。
 */
static node* get_node(queue *q,int index)
{
    if (index < 0 || index >= q->count)
        return NULL;
    
    if (index <= (q->count / 2))
    {
        int i = 0;
        node *pnode = q->phead->next;
        while ((i++) < index)
            pnode = pnode->next;
        return pnode;
    }
    int j = 0;
    int rindex = q->count - index - 1;
    node *rnode = q->phead->prev;
    while ((j++) < rindex)
        rnode = rnode->prev;

    return rnode;
}

/**
 根據index獲取改鏈表節點里面的存儲內容

 @param q 鏈表
 @param index 鏈表節點下標
 @return 該下標元素節點中存儲的內容
 */
void* queue_get(queue *q,int index)
{
    SDL_LockMutex(q->mutex);
    node *pindex = get_node(q,index);
    if (!pindex)
    {
        SDL_UnlockMutex(q->mutex);
        return NULL;
    }
    SDL_UnlockMutex(q->mutex);
    return pindex->para;
}

/**
 獲取該鏈表第一個節點存儲的內容

 @param q 鏈表
 @return 該鏈表的某個節點內容
 */
void* queue_get_first(queue *q)
{
    return queue_get(q,0);
}

/**
 獲取該鏈表最后一個節點存儲的內容

 @param q 鏈表
 @return 該鏈表的某個節點內容
 */
void* queue_get_last(queue *q)
{
    return queue_get(q,q->count - 1);
}

/**
 存儲一個元素到鏈表

 @param q 要存儲到的鏈表
 @param para 要存儲的內容
 @return success return 0 ,fail return -1
 */
int queue_append_last(queue *q,void *para)
{
    SDL_LockMutex(q->mutex);
    node *pnode = create_node(para);
    if (!pnode)
    {
        SDL_UnlockMutex(q->mutex);
        return -1;
    }
    pnode->next = q->phead;
    pnode->prev = q->phead->prev;
    q->phead->prev->next = pnode;
    q->phead->prev = pnode;
    q->last_node = pnode;
    q->count++;
    SDL_UnlockMutex(q->mutex);
    return 0;
}

/**
 根據index 刪除鏈表的元素節點

 @param q 鏈表
 @param index 要刪除的節點下標
 @return success return 0 ,fail return -1
 */
int queue_delete(queue *q,int index)
{
    SDL_LockMutex(q->mutex);
    node *pindex = get_node(q,index);
    if (!pindex)
    {
        SDL_UnlockMutex(q->mutex);
        return -1;
    }
    pindex->next->prev = pindex->prev;
    pindex->prev->next = pindex->next;
    free(pindex);
    q->count--;
    SDL_UnlockMutex(q->mutex);
    return 0;
}

/**
 刪除該鏈表第一個節點

 @param q 鏈表
 @return success return 0 ,fail return -1
 */
int queue_delete_first(queue *q)
{
    return queue_delete(q,0);
}
/**
 刪除該鏈表最后一個節點
 
 @param q 鏈表
 @return success return 0 ,fail return -1
 */
int queue_delete_last(queue *q)
{
    return queue_delete(q, q->count - 1);
}

/**
 銷毀鏈表

 @param q 鏈表
 @return success return 0 ,fail return -1
 */
int destroy_queue(queue *q)
{
    SDL_LockMutex(q->mutex);
    if (!q->phead)
    {
        SDL_UnlockMutex(q->mutex);
        return -1;
    }
    node *pnode = q->phead->next;
    node *ptmp = NULL;
    while (pnode != q->phead)
    {
        ptmp = pnode;
        pnode = pnode->next;
        free(ptmp);
    }
    free(q->phead);
    q->phead = NULL;
    q->last_node = NULL;
    q->first_node = NULL;
    q->count = 0;
    SDL_UnlockMutex(q->mutex);
    SDL_DestroyMutex(q->mutex);
    SDL_DestroyCond(q->cond);
    return 0;
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

推薦閱讀更多精彩內容