1. 程式人生 > >黑馬《linux系統程式設計》學習筆記(從76到80)

黑馬《linux系統程式設計》學習筆記(從76到80)

七十六. 執行緒同步的思想

七十七. 互斥鎖相關的函式

七十八. 使用互斥鎖,實現執行緒同步

首先是 pthread_mutex.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>

#define MAX 10000
// 全域性變數
int number;
//建立一把互斥鎖
pthread_mutex_t mutex;

// 執行緒處理函式
void* funcA_num(void* arg)
{
    for(int i=0; i<MAX; ++i)
    {
                //訪問全域性變數之前加鎖
                //如果mutex被鎖上了,程式碼阻塞在當前位置
                pthread_mutex_lock(&mutex);
        int cur = number;
        cur++;
        number = cur;
        printf("Thread A, id = %lu, number = %d\n", pthread_self(), number);
                //解鎖
                pthread_mutex_unlock(&mutex);
        usleep(10);
    }

    return NULL;
}

void* funcB_num(void* arg)
{
    for(int i=0; i<MAX; ++i)
    {
                pthread_mutex_lock(&mutex);
        int cur = number;
        cur++;
        number = cur;
        printf("Thread B, id = %lu, number = %d\n", pthread_self(), number);
                pthread_mutex_unlock(&mutex);
        usleep(10);
    }

    return NULL;
}

int main(int argc, const char* argv[])
{
    pthread_t p1, p2;
        //初始化互斥鎖
        pthread_mutex_init(&mutex,NULL);

    // 建立兩個子執行緒
    pthread_create(&p1, NULL, funcA_num, NULL);
    pthread_create(&p2, NULL, funcB_num, NULL);

    // 阻塞,資源回收
    pthread_join(p1, NULL);
    pthread_join(p2, NULL);

        //釋放互斥鎖資源
        pthread_mutex_destroy(&mutex);

    return 0;
}

執行一下

[[email protected]_0_15_centos 8Day]# vim pthread_mutex_.c
[[email protected]_0_15_centos 8Day]# gcc pthread_mutex_.c -o p_m -std=gnu99 -lpthread
[[email protected]_0_15_centos 8Day]# ls
a.out                  p_e  process_r.c     pthread_create.c  pthread_mutex_.c
deamon.c               p_j  process_work.c  pthread_exit.c    pthread_uncle.c
loop_pthread_create.c  p_m  pthread_attr.c  pthread_join.c    setsid.c
[
[email protected]
_0_15_centos 8Day]# ./p_m Thread A, id = 140197652313856, number = 1 Thread B, id = 140197643921152, number = 2 Thread A, id = 140197652313856, number = 3 Thread B, id = 140197643921152, number = 4 Thread A, id = 140197652313856, number = 5 Thread A, id = 140197652313856, number = 6 Thread B, id = 140197643921152, number = 7 Thread A, id = 140197652313856, number = 8 Thread B, id = 140197643921152, number = 9 Thread B, id = 140197643921152, number = 10 Thread A, id = 140197652313856, number = 11 Thread B, id = 140197643921152, number = 12 Thread A, id = 140197652313856, number = 13 Thread A, id = 140197652313856, number = 14 …………………… …………………… Thread A, id = 140197652313856, number = 1696 Thread B, id = 140197643921152, number = 1697 Thread B, id = 140197643921152, number = 1698 Thread A, id = 140197652313856, number = 1699 Thread B, id = 140197643921152, number = 1700 Thread A, id = 140197652313856, number = 1701 Thread A, id = 140197652313856, number = 1702 Thread B, id = 140197643921152, number = 1703 Thread A, id = 140197652313856, number = 1704 Thread B, id = 140197643921152, number = 1705 Thread B, id = 140197643921152, number = 1706 Thread A, id = 140197652313856, number = 1707 Thread B, id = 140197643921152, number = 1708 Thread A, id = 140197652313856, number = 1709 Thread A, id = 140197652313856, number = 1710 Thread B, id = 140197643921152, number = 1711 Thread A, id = 140197652313856, number = 1712 Thread B, id = 140197643921152, number = 1713 Thread B, id = 140197643921152, number = 1714 Thread A, id = 140197652313856, number = 1715 Thread B, id = 140197643921152, number = 1716 Thread A, id = 140197652313856, number = 1717 Thread A, id = 140197652313856, number = 1718 Thread B, id = 140197643921152, number = 1719 Thread A, id = 140197652313856, number = 1720 Thread B, id = 140197643921152, number = 1721 Thread B, id = 140197643921152, number = 1722 Thread A, id = 140197652313856, number = 1723 Thread B, id = 140197643921152, number = 1724 Thread A, id = 140197652313856, number = 1725 Thread B, id = 140197643921152, number = 1726 Thread A, id = 140197652313856, number = 1727 Thread A, id = 140197652313856, number = 1728 Thread B, id = 140197643921152, number = 1729 Thread A, id = 140197652313856, number = 1730 Thread B, id = 140197643921152, number = 1731

七十九. 學習目標

八十. 互斥鎖使用的時候需要注意的問題