当前位置: 首页 > news >正文

北京微网站建设公司哪家好国内新闻大事20条

北京微网站建设公司哪家好,国内新闻大事20条,桌面网站怎么做,自己可以做防伪网站吗在前面的文章中提及到,一个进程中的多个线程是共享同一段资源的,由于线程对资源的竞争引出了锁。其中mutex是一种简单的加锁方法,这个互斥锁只有两种状态,那就是上锁和解锁,可以把互斥锁看作是某种意义上的全局变量。在…

 在前面的文章中提及到,一个进程中的多个线程是共享同一段资源的,由于线程对资源的竞争引出了锁。其中mutex是一种简单的加锁方法,这个互斥锁只有两种状态,那就是上锁和解锁,可以把互斥锁看作是某种意义上的全局变量。在某一时刻,只能有一个线程取得这个互斥上的锁,拥有上锁状态的线程可以对共享资源进行操作,而其他线程在该线程未解锁之前,够会被挂起,直到上锁的线程解开锁。可以这么说,互斥锁使得共享资源按序的在各个线程上操作。

互斥锁的操作主要包括互斥锁初始化、上锁、判断上锁、解锁、摧毁互斥锁。其中互斥锁可以分为快速互斥锁、递归互斥锁这检错互斥锁。这三种锁的区别主要在于其他未占有互斥锁的线程在希望得到互斥锁时是否需要等待挂起。快速锁是指调用线程会阻塞直到线程锁得到解锁为止。递归锁能够成功地返回并且增加调用线程在互斥上的加锁次数,比如一个链表在进行插入的操作时,可以进行查找的操作。检错锁则为快速互斥锁的非阻塞版本,它会立即返回并返回一个错误的信息。

1、函数简义。

(1)pthread_mutex_init

头文件:                  <pthread.h>

函数原型:               int pthread_mutex_init (pthread_mutex_t* mutex,

                                                                         const pthread_mutexattr_t* mutexattr);

函数传入值:            mutex:互斥锁。

                              mutexattr:PTHREAD_MUTEX_INITIALIZER:创建快速互斥锁。

                                               PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:创建递归互斥锁。

                                               PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:创建检错互斥锁。

函数返回值:            成功:0

                              出错:-1

(2)上锁函数:

int pthread_mutex_lock(pthread_mutex_t* mutex);

int pthread_mutex_trylock (pthread_mutex_t* mutex);

int pthread_mutex_unlock (pthread_mutex_t* mutex);

int pthread_mutex_destroy (pthread_mutex_t* mutex);

函数传入值:            mutex:互斥锁。

函数返回值:            同上。

2、互斥锁实现。

view plaincopy to clipboardprint?
#include <stdio.h>  
#include <stdlib.h>  
#include <pthread.h>  
#include <errno.h>  
#include <unistd.h>  
 
#define  return_if_fail(p,v)  /  
         if(!p){printf("[%s]:func error!/n",__func__);return;}  
   
typedef struct _PrivInfo  
{  
   pthread_mutex_t mutex;  
   int lock_var;  
   time_t end_time;  
}PrivInfo;  
 
static void info_init (PrivInfo* thiz);  
static void pthread_func_1 (PrivInfo* thiz);  
static void pthread_func_2 (PrivInfo* thiz);  
 
int main (int argc, char** argv)  
{  
   pthread_t pt_1 = 0;  
   pthread_t pt_2 = 0;  
   int ret = 0;  
   PrivInfo* thiz = NULL;  
 
   thiz = (PrivInfo*)malloc (sizeof (PrivInfo));  
   if (thiz == NULL)  
   {  
      return -1;  
   }  
 
   info_init (PrivInfo* thiz);  
 
   ret = pthread_create (&pt_1, NULL, pthread_func_1, thiz);  
   if (ret != 0)  
   {  
     perror ("pthread_1_create:");  
   }  
     
   ret = pthread_create (&pt_2, NULL, pthread_func_2, thiz);  
   {  
     perror ("pthread_2_create:");  
   }  
 
   pthread_join (pt_1, NULL):  
   pthread_join (pt_2, NULL);  
 
   pthread_mutex_destroy (&thiz->mutex);  
     
   free (thiz);  
   thiz = NULL;  
 
   return 0;  
}  
 
static void info_init (PrivInfo* thiz)  
{  
   return_if_fail (thiz != NULL);  
 
   thiz->lock_var = 0;  
   thiz->end_time = time (NULL) + 10;  
 
   pthread_mutex_init (&mutex, NULL);  
 
   return;  
}  
 
static void pthread_func_1 (PrivInfo* thiz)  
{  
   return_if_fail (thiz != NULL);  
 
   int i = 0;  
   int ret = 0;  
 
   while (time (NULL) < thiz->end_time)  
   {  
     ret  = pthread_mutex_lock (thiz->mutex);  
     if (ret != 0)  
     {  
         perror ("[%s]pthread_mutex_lock:",__func__);  
     }  
     else 
     {  
       printf ("pthread1:pthread1 lock the variable!/n");  
     }  
 
     for (; i < 2; i++)  
     {  
        sleep (1);  
        thiz->lock_var ++;  
     }  
 
     ret = pthread_mutex_unlock (&thiz->mutex);  
     if (ret != 0)  
     {  
        perror ("[%s]pthread_mutex_unlock:", __func__);  
     }  
     else 
     {  
        printf ("pthread1: pthread1 unlock the variable./n");  
     }       
   }  
 
   return;  
}  
 
static void pthread_func_2 (PrivInfo* thiz)  
{  
   return_if_fail (thiz != NULL);  
 
   int ret = 0;  
     
   while (time (NULL) < thiz->end_time)  
   {  
      ret = pthread_mutex_trylock (&thiz->mutex);  
      if (ret == EBUSY)  
      {  
         printf ("pthread2:the variable is locked by thread1./n");  
      }  
      else 
      {  
         if (ret != 0)  
         {  
             perror ("[%s]pthread2_mutex_trylock:", __func__);   
         }  
         else 
         {  
            printf ("pthread2: pthread2 lock the variable./n");  
         }           
        
 
         ret = pthread_mutex_unlock (&thiz->mutex);  
         if (ret != 0)  
         {  
           perror ("[%s]pthread_mutex_lock:", __func__);  
         }  
 
         sleep (3);  
      }  
    }  

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>

#define  return_if_fail(p,v)  /
         if(!p){printf("[%s]:func error!/n",__func__);return;}
 
typedef struct _PrivInfo
{
   pthread_mutex_t mutex;
   int lock_var;
   time_t end_time;
}PrivInfo;

static void info_init (PrivInfo* thiz);
static void pthread_func_1 (PrivInfo* thiz);
static void pthread_func_2 (PrivInfo* thiz);

int main (int argc, char** argv)
{
   pthread_t pt_1 = 0;
   pthread_t pt_2 = 0;
   int ret = 0;
   PrivInfo* thiz = NULL;

   thiz = (PrivInfo*)malloc (sizeof (PrivInfo));
   if (thiz == NULL)
   {
      return -1;
   }

   info_init (PrivInfo* thiz);

   ret = pthread_create (&pt_1, NULL, pthread_func_1, thiz);
   if (ret != 0)
   {
     perror ("pthread_1_create:");
   }
  
   ret = pthread_create (&pt_2, NULL, pthread_func_2, thiz);
   {
     perror ("pthread_2_create:");
   }

   pthread_join (pt_1, NULL):
   pthread_join (pt_2, NULL);

   pthread_mutex_destroy (&thiz->mutex);
  
   free (thiz);
   thiz = NULL;

   return 0;
}

static void info_init (PrivInfo* thiz)
{
   return_if_fail (thiz != NULL);

   thiz->lock_var = 0;
   thiz->end_time = time (NULL) + 10;

   pthread_mutex_init (&mutex, NULL);

   return;
}

static void pthread_func_1 (PrivInfo* thiz)
{
   return_if_fail (thiz != NULL);

   int i = 0;
   int ret = 0;

   while (time (NULL) < thiz->end_time)
   {
     ret  = pthread_mutex_lock (thiz->mutex);
     if (ret != 0)
     {
         perror ("[%s]pthread_mutex_lock:",__func__);
     }
     else
     {
       printf ("pthread1:pthread1 lock the variable!/n");
     }

     for (; i < 2; i++)
     {
        sleep (1);
        thiz->lock_var ++;
     }

     ret = pthread_mutex_unlock (&thiz->mutex);
     if (ret != 0)
     {
        perror ("[%s]pthread_mutex_unlock:", __func__);
     }
     else
     {
        printf ("pthread1: pthread1 unlock the variable./n");
     }    
   }

   return;
}

static void pthread_func_2 (PrivInfo* thiz)
{
   return_if_fail (thiz != NULL);

   int ret = 0;
  
   while (time (NULL) < thiz->end_time)
   {
      ret = pthread_mutex_trylock (&thiz->mutex);
      if (ret == EBUSY)
      {
         printf ("pthread2:the variable is locked by thread1./n");
      }
      else
      {
         if (ret != 0)
         {
             perror ("[%s]pthread2_mutex_trylock:", __func__);
         }
         else
         {
            printf ("pthread2: pthread2 lock the variable./n");
         }

         ret = pthread_mutex_unlock (&thiz->mutex);
         if (ret != 0)
         {
           perror ("[%s]pthread_mutex_lock:", __func__);
         }

         sleep (3);
      }
    }
}

在上例的中,是对变量lock_var的读写进行加锁,线程一是对其进行写,在写的时候,线程二不能对其进行读。直到线程一解锁,线程二得到互斥锁,才能进行读。同样,在线程二进行读的时候,线程一不能进行写。

今天讲述到这里,下一篇文章将讲述怎样通过信号量来达到互斥锁的效果。

 

http://www.lbrq.cn/news/2628721.html

相关文章:

  • 郑州国际装备制造业会展网站的建设营销的手段和方法
  • wordpress简题浙江关键词优化
  • 常州企业网站建设价格seo是什么姓
  • 做汽车新闻哪个网站好网推项目平台
  • 旅游门户网站源码怎么做的软文优化
  • 公司网站建设设计软文写手接单平台
  • 广告网站设计公司 作用外贸推广平台排名
  • 做投资的网站久久seo综合查询
  • 焦作市建设银行网站谷歌收录查询工具
  • 平顶山河南网站建设简述什么是网络营销
  • 广州上宏网站建设如何建立网站
  • 电话网络营销是什么上海高玩seo
  • 网络规划与设计需求分析学seo需要多久
  • bootstrap风格网站seo最新
  • 做网站图片太大好吗seo的定义
  • 便捷的大连网站建设123网址之家
  • 最新永久4虎最新人口谷歌优化技巧
  • 17做网站广州起做网店让百度收录自己的网站
  • 沧州网站建设推广凡科网站建站教程
  • 外贸soho怎么建网站二十条优化
  • 手机上怎么制作网站吗windows优化大师会员兑换码
  • 网站怎么做劫持老域名购买
  • 万网注册域名的步骤深圳网站搜索优化工具
  • 怎么在.Net中做团购网站北京做的好的seo公司
  • 北京网站建设手机app电子商务女装标题优化关键词
  • 深圳网页开发快速排名优化
  • 有谁有做卫生纸巾的网站东莞seo关键词排名优化排名
  • 56网站可以做电子相册贴吧推广400一个月
  • 万众城网站建设成都百度推广公司联系电话
  • 网站公安备案 查询百度知道合伙人官网登录入口
  • 网页前端CSS实现表格3行平均分配高度,或者用div Flexbox布局
  • 最优化中常见的优化理论
  • 类内部方法调用,自注入避免AOP失效
  • 【Spring Boot 】Spring Boot + OpenAI API 万能集成模板,实现快速集成AI
  • Claude Code深度操作指南:从零到专家的AI编程助手实战
  • 音视频学习笔记