• multi-threads synchronization use conditional mutex


    #include <pthread.h>
    int thread_flag;
    pthread_cond_t thread_flag_cv;
    pthread_mutex_t thread_flag_mutex;
    void initialize_flag ()
    {
    /* Initialize the mutex and condition variable.
    pthread_mutex_init (&thread_flag_mutex, NULL);
    pthread_cond_init (&thread_flag_cv, NULL);
    /* Initialize the flag value. */
    thread_flag = 0;
    }
    */
    /* Calls do_work repeatedly while the thread flag is set; blocks if
    the flag is clear. */
    void* thread_function (void* thread_arg)
    {
    /* Loop infinitely. */
    while (1) {
    /* Lock the mutex before accessing the flag value. */
    pthread_mutex_lock (&thread_flag_mutex);
    while (!thread_flag)
    /* The flag is clear. Wait for a signal on the condition
    variable, indicating that the flag value has changed. When the
    signal arrives and this thread unblocks, loop and check the
    flag again. */
    pthread_cond_wait (&thread_flag_cv, &thread_flag_mutex);
    /* When we’ve gotten here, we know the flag must be set. Unlock
    the mutex. */
    pthread_mutex_unlock (&thread_flag_mutex);
    /* Do some work. */
    do_work ();
    }
    return NULL;
    }
    /* Sets the value of the thread flag to FLAG_VALUE.
    */
    void set_thread_flag (int flag_value)
    {
    /* Lock the mutex before accessing the flag value. */
    pthread_mutex_lock (&thread_flag_mutex);
    /* Set the flag value, and then signal in case thread_function is
    blocked, waiting for the flag to become set. However,
    thread_function can’t actually check the flag until the mutex is
    unlocked. */
    thread_flag = flag_value;
    pthread_cond_signal (&thread_flag_cv);
    /* Unlock the mutex. */
    pthread_mutex_unlock (&thread_flag_mutex);
    }
  • 相关阅读:
    Yii2 数据操作Query Builder
    Yii2.0 rules验证规则大全
    git错误解决 -- 小结
    为什么结构化编程、面向对象编程、软件工程、架构设计最后没有成为软件领域的银弹
    系统和子系统、架构和框架、模块和组件
    MyBatis实战之动态SQL
    Controller如何写的更简化
    MyBatis实战之映射器
    WiFi密码忘记了怎么办之解决方案
    Linux常用监控服务器性能命令
  • 原文地址:https://www.cnblogs.com/feika/p/3614517.html
Copyright © 2020-2023  润新知