日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

          Linux多線程同步方法

          作者: 時間:2011-08-11 來源:網(wǎng)絡(luò) 收藏

          以下是的幾種方式:

          本文引用地址:http://yuyingmama.com.cn/article/150368.htm

          1、 互斥量。

          通過使用pthread的互斥接口保護(hù)數(shù)據(jù),確保同一時間只有一個訪問數(shù)據(jù)?;コ饬繌谋举|(zhì)上講是一把鎖,在訪問共享資源前對互斥量進(jìn)行加鎖,在訪問完成后釋放互斥量上的鎖。如下例所示,就是互斥量對共享數(shù)據(jù)的操作:

          #include stdio.h>
          #include pthread.h>
          int value = 5;//共享變量
          pthread_mutex_t mutex;//互斥變量
          void *mythread1();
          void mainshow();
          int main()
          {
          int retval;
          pthread_t tid1;
          retval = pthread_create(tid1,NULL,mythread1,value);//創(chuàng)建
          if(retval != 0){printf(“Can not create mythread1n”);
          mainshow();
          retval = pthread_join(tid1,NULL);//等待線程mythread1結(jié)束
          if(retval != 0){printf(“Can not join with mythread.n”);
          printf(“value = %dn”,value);
          return 0;
          }

          void *mythread1()
          {
          int retval;
          retval = pthread_mutex_lock(mutex);//上鎖
          value = value + 1;//對共享變量的操作
          printf(value = %dn,value);
          retval = pthread_mutex_unlock(mutex);//解鎖
          pthread_exit((void *)0);
          }


          void myshow()
          {
          int retval;
          retval = pthread_mutex_lock(mutex);//上鎖
          value = value + 1;//對共享變量的操作
          printf(“value = %dn”,value);
          pthread_mutex_unlock(mutex);//解鎖
          }

          2、信號量

          該信號量是Posix提供的基于內(nèi)存的信號量,它們由應(yīng)用程序分配信號量的內(nèi)存空間。如下例所示,就是信號量對共享數(shù)據(jù)的操作:

          #include stdio.h>
          #include pthread.h>
          #include semaphore.h>
          int value = 5;
          sem_t sem1,sem2;
          void mainshow();
          void *mythread();
          int main()
          {
          int retval;
          pthread_t tid;
          retval = sem_init(sem1,0,0);
          retval = sem_init(sem2,0,1);
          retval =pthread_create(tid,NULL,mythread,NULL);
          mainshow();
          pthread_join(tid,NULL);


          printf(value3 = %dn,value);
          return 0;
          }


          void *mythread()
          {
          int retval;
          retval = sem_wait(sem1);
          value = value + 1;
          printf(value1 = %dn,value);
          retval = sem_post(sem2);
          pthread_exit((void *) 0);
          }


          void mainshow()
          {
          int retval;
          retval = sem_wait(sem2);
          value = value + 1;
          printf(value2 = %dn,value);
          retval = sem_post(sem1);
          }

          linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

          linux相關(guān)文章:linux教程




          關(guān)鍵詞: 方法 同步 線程 Linux

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉