zoukankan      html  css  js  c++  java
  • 线程间通信--互斥量

    
    
     1 /*******************************************************************************
     2 * 版权所有:
     3 * 模 块 名:
     4 * 文 件 名:pthread4_addnum.c
     5 * 实现功能:
     6 * 作    者:XYZ
     7 * 版    本:V1.0
     8 * 日    期:2013.07.26
     9 * 其他说明:无
    10 ********************************************************************************/
    11 // pthread4_addnum.c
    12 #include<stdio.h>
    13 #include<unistd.h>
    14 #include<stdlib.h>
    15 #include<pthread.h>
    16 
    17 void *thread_function(void *arg);
    18 pthread_mutex_t num_mutex;
    19 #define PTHREAD_NUM        2
    20 #define NUM        30
    21 int cnt = 0;
    22 
    23 int main()
    24 {
    25     int res;
    26     pthread_t thread_id[PTHREAD_NUM];
    27     void *thread_result;
    28     res = pthread_mutex_init(&num_mutex, NULL);
    29     if (res != 0)
    30     {
    31         perror("Mutex initialization failed
    ");
    32         exit(EXIT_FAILURE);
    33     }
    34     
    35     int i ;
    36 
    37     for (i=0; i<PTHREAD_NUM; ++i)
    38     {
    39         pthread_create(&thread_id[i], NULL, thread_function, (void*)thread_id[i]);
    40     }
    41 
    42 #if 1
    43     for (i=0; i<NUM; ++i)
    44     {
    45         pthread_mutex_lock(&num_mutex);
    46         fprintf(stderr, "%s thread ID %lu before cnt=%d ",__FUNCTION__,pthread_self(), cnt);
    47         cnt += 1;
    48         fprintf(stderr, "after cnt=%d
    
    ", cnt);
    49         pthread_mutex_unlock(&num_mutex);
    50         sched_yield();
    51         usleep(1);
    52     }
    53 #endif
    54 
    55     for (i=0; i<PTHREAD_NUM; ++i)
    56     {
    57         pthread_join(thread_id[i], NULL);
    58     }
    59     pthread_mutex_destroy(&num_mutex);    
    60 
    61     pthread_exit(0);
    62 }
    63 
    64 void *thread_function(void *arg)
    65 {
    66     int i;
    67     for (i=0; i<NUM; ++i)
    68     {
    69         pthread_mutex_lock(&num_mutex);
    70         //fprintf(stderr, "thread ID %d befor cnt=%d ",(int)arg, cnt);
    71         fprintf(stderr, "%s thread ID %lu before cnt=%d ",__FUNCTION__,pthread_self(), cnt);
    72         cnt += 1;
    73         fprintf(stderr, "after cnt=%d
    
    ", cnt);
    74         pthread_mutex_unlock(&num_mutex);
    75         sched_yield();
    76         usleep(1);
    77     }
    78     pthread_exit(NULL);
    79 }
    
    
    1.pthread_mutex_init(&work_mutex, NULL);  //init the mutex
      pthread_mutex_destroy(&work_mutex);
    pthread_mutex_destroy,  pthread_mutex_init  -  destroy and initialize a mutex
    #include <pthread.h>
    int pthread_mutex_destroy(pthread_mutex_t *mutex);
    int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 

    The pthread_mutex_init() function shall initialize the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; Only mutex itself may be used for performing synchronization. The result of referring to copies of mutex in calls to pthread_mutex_lock(), pthread_mutex_trylock(), pthread_mutex_unlock(), and pthread_mutex_destroy() is undefined.Attempting to initialize an already initialized mutex results in undefined behavior.
    In cases where default mutex  attributes  are  appropriate,  the  macro PTHREAD_MUTEX_INITIALIZER  can  be  used to initialize mutexes that are statically allocated. The effect shall be equivalent  to  dynamic  initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.
    
    If successful,  the  pthread_mutex_destroy()  and  pthread_mutex_init() functions  shall  return  zero;  otherwise,  an  error  number shall be returned to indicate the error.
    
    2.pthread_mutex_lock(&work_mutex);
      pthread_mutex_unlock(&work_mutex);
    pthread_mutex_lock,  pthread_mutex_trylock, pthread_mutex_unlock - lock and unlock a mutex
    #include <pthread.h>
    int pthread_mutex_lock(pthread_mutex_t *mutex);
    int pthread_mutex_trylock(pthread_mutex_t *mutex);
    int pthread_mutex_unlock(pthread_mutex_t *mutex);
    The mutex object referenced by mutex shall be locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread shall block until the mutex becomes available. This operation shall return with the mutex object referenced by mutex in the locked state with the calling thread as its owner. If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not be provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results. The pthread_mutex_trylock() function shall be equivalent to pthread_mutex_lock(), except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call shall return immediately. The pthread_mutex_unlock() function shall release the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex's type attribute. If successful,the pthread_mutex_lock() and pthread_mutex_unlock() functions shall return zero; otherwise, an error number shall be returned to indicate the error. The pthread_mutex_trylock() function shall return zero if a lock on the mutex object referenced by mutex is acquired. Otherwise, an error number is returned to indicate the error


  • 相关阅读:
    ReactNative 常见红屏黄屏及终端报错
    ReactNative 常见红屏黄屏及终端报错
    React Native小白入门学习路径——二
    学习TINY需要多长时间?
    在Eclipse中导入Tiny工程,有下面的错误,是什么原因?
    为什么我写的page页面无法渲染
    Tiny框架的应用定位
    为什么编译tiny工程出错,提示"不兼容的类型"
    Tiny工程可以配置多个application.xml吗
    为什么web应用在tomcat启动时报java.lang.ClassCastException异常?
  • 原文地址:https://www.cnblogs.com/xiao13149920yanyan/p/3229457.html
Copyright © 2011-2022 走看看