隨筆-204  評論-149  文章-0  trackbacks-0

          信號量

          基本操作

          #include <semaphore.h>
          int sem_init(sem_t *sem, int pshared, unsigned int value);
          int sem_wait(sem_t *sem);          /* P(sem), wait(sem) */
          int sem_post(sem_t *sem);          /* V(sem), signal(sem) */

          int sem_getvalue(sem_t *sem, int *sval);
          int sem_trywait(sem_t *sem);

          int sem_destroy(sem_t *sem);       /* undo sem_init() */

          /* named semaphores - these are less useful here */
          sem_t 
          *sem_open(  );
          int sem_close(sem_t *sem);
          int sem_unlink(const char *name);

          互斥量

          基本操作

          #include <pthread.h>
          int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
          pthread_mutex_t mutex 
          = PTHREAD_MUTEX_INITIALIZER;
          int pthread_mutex_lock(pthread_mutex_t *mutex);
          int pthread_mutex_unlock(pthread_mutex_t *mutex);

          int pthread_mutex_trylock(pthread_mutex_t *mutex);
          int pthread_mutex_destroy(pthread_mutex_t *mutex); 

          條件變量

          一種信號機制

          基本操作

          #include <pthread.h>
          int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
          pthread_cond_t cond 
          = PTHREAD_COND_INITIALIZER;
          int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
          int pthread_cond_signal(pthread_cond_t *cond);

          int pthread_cond_timedwait(  );
          int pthread_cond_broadcast(pthread_cond_t *cond);
          int pthread_cond_destroy(pthread_cond_t *cond); 

          互斥量A保護條件變量B

          //等待方
          pthread_mutex_lock(&A);
          while(){//檢查條件是否滿足
              pthread_cond_wait(&B, &A);
          }

            
          /* wait會隱式解鎖A */
            
          /* wait后A會被隱式鎖住A */
          pthread_mutex_unlock(
          &A);

          //通知方
          pthread_mutex_lock(&A);
          pthread_cond_signal(
          &B);
          pthread_mutex_unlock(
          &A);
          posted on 2009-07-05 01:02 Frank_Fang 閱讀(614) 評論(0)  編輯  收藏 所屬分類: Linux | ACE網絡編程
          主站蜘蛛池模板: 犍为县| 吉隆县| 清远市| 闽清县| 探索| 登封市| 涟源市| 那曲县| 伊吾县| 乐至县| 东莞市| 农安县| 礼泉县| 临洮县| 梁河县| 长沙县| 永清县| 云浮市| 玛纳斯县| 思南县| 萝北县| 大新县| 满城县| 四会市| 涞源县| 慈溪市| 江门市| 长顺县| 碌曲县| 孟连| 临猗县| 柳林县| 博客| 邯郸市| 清苑县| 贵南县| 金川县| 张家港市| 成武县| 高淳县| 沾益县|