zoukankan      html  css  js  c++  java
  • Linux线程同步---互斥锁

    线程中互斥锁使用的步骤与信号量相似!

    1、首先定义互斥锁变量,并初始化

    pthread_mutex_t mutex_lock;
    pthread_mutex_init(&mutex_lock,NULL);
    2、在操作前对互斥量进行加锁操作

    pthread_mutex_lock(&mutex_lock);
    3、操作完毕后进行解锁操作

    pthread_mutex_unlock(&mutex_lock);

    所有操作均在加锁和解锁操作之间进行,保证同时仅仅对有一个操作对关键变量或是区域进行操作。

    下面是自己写的一个小程序,对主函数对变量自加操作,而线程函数进行自减操作。

    (该程序有个小bug,看官可执行运行一下,就知道结果了,高手一看就知道问题在哪里,哈哈!卖个关子!)

    pthread_mutex_lock(&mutex_lock);对某个互斥锁变量进行加锁操作时,当该变量已经被另外一个线程锁住时,该线程会被阻塞(英文原文为block),可以理解为休眠。
    由操作系统基本原理可知,当进程处于就绪状态时,才会获得处理机的时间片,换句话说就是能能被调度到处理机上执行,而阻塞状态则不能。

    因此一个线程被阻塞后,将不会执行,处于等待状态,通常是等待某个事件发生或到来。因此,一个对已经加锁的变量进行加锁的线程将被阻塞,其只能等待另外一个线程解锁,才能继续执行。

     1 #include<stdio.h>
     2 #include<unistd.h>
     3 #include<stdlib.h>
     4 #include<pthread.h>
     5 #include<string.h>
     6 #include<errno.h>
     7 
     8 void *thread_function(void *arg);
     9 pthread_mutex_t mutex_lock;
    10 int globaltmp=0;
    11 int main(){
    12     int res;
    13     pthread_t pthread_tmp;
    14     pthread_mutex_init(&mutex_lock,NULL);
    15     pthread_mutex_lock(&mutex_lock);
    16     res = pthread_create(&pthread_tmp,NULL,thread_function,NULL);
    17     if(res != 0){
    18         perror("thread creation failed!");
    19         exit(EXIT_FAILURE);
    20     }
    21     while(1){
    22         pthread_mutex_unlock(&mutex_lock);
    23         sleep(2);
    24         globaltmp++;
    25         printf("in the main func,globaltmp=%d
    ",globaltmp);
    26          res = pthread_mutex_lock(&mutex_lock);        
    27     //    if(res == EDEADLK)
    28         {
    29         //    printf("it has been locked in the thread!
    ");
    30         }
    31     //    printf(" main func res =%d
    ",res);
    32         sleep(2);
    33     }
    34     res = pthread_join(pthread_tmp,NULL);
    35     if(res != 0 ){
    36         perror("thread join is failure");
    37         exit(EXIT_FAILURE);
    38     }
    39     printf("thread joined!
    ");
    40     pthread_mutex_destroy(&mutex_lock);
    41     return 0;
    42 }
    43 void *thread_function(void *arg){
    44     int res ;
    45     res = pthread_mutex_lock(&mutex_lock);
    46     if(res == EDEADLK)
    47         printf("it has been locked in the main!
    ");
    48     while(1)
    49     {
    50         pthread_mutex_unlock(&mutex_lock);
    51         sleep(1);
    52         globaltmp--;
    53         printf("in the pthread func,globaltmp=%d
    ",globaltmp);
    54     //    printf("I am in the pthread func
    ");
    55         res = pthread_mutex_lock(&mutex_lock);
    56 //        printf("thread func res=%d
    ",res);
    57     //    if(res == EDEADLK)
    58         //    printf("it has been locked in the main!
    ");
    59         sleep(1);
    60     }
    61     pthread_exit(NULL);
    62 }    
  • 相关阅读:
    sqlite3 学习
    解释内存中的栈(stack)、堆(heap)和静态区(static area)的用法
    HTML基本语法
    linux shell脚本、命令学习
    laravel 缓存相关常用操作
    【扩展推荐】Intervention/image 图片处理
    【Laravel】 常用的artisian命令
    laravel开发调试工具laravel-debugbar的安装
    为PhpStorm添加Laravel 代码智能提示功能
    laravel5.5 安装
  • 原文地址:https://www.cnblogs.com/farbeyond/p/4482859.html
Copyright © 2011-2022 走看看