zoukankan      html  css  js  c++  java
  • posix多线程有感线程高级编程(pthread_once)

        有时候我们需要对一些posix变量只进行一次初始化,如线程键(我下面会讲到)。如果我们进行多次初始化程序就会出现错误。

        在传统的顺序编程中,一次性初始化经常通过使用布尔变量来管理。控制变量被静态初始化为0,而任何依赖于初始化的代码都能测试该变量。如果变量值仍然为0,则它能实行初始化,然后将变量置为1。以后检查的代码将跳过初始化。

        但是在多线程程序设计中,事情就变的复杂的多。如果多个线程并发地执行初始化序列代码,可能有2个线程发现控制变量为0,并且都实行初始化,而该过程本该仅仅执行一次。

    如果我们需要对一个posix变量静态的初始化,可使用的方法是用一个互斥量对该变量的初始话进行控制。但有时候我们需要对该变量进行动态初始化,pthread_once就会方便的多。 

    函数原形:

    pthread_once_t once_control=PTHREAD_ONCE_INIT;
    int pthread_once(pthread_once_t *once_control,void (*init_routine)(void));

    参数:

    once_control         控制变量

    init_routine         初始化函数

    返回值:

       若成功返回0,若失败返回错误编号。

       类型为pthread_once_t的变量是一个控制变量。控制变量必须使用PTHREAD_ONCE_INIT宏静态地初始化。

       pthread_once函数首先检查控制变量,判断是否已经完成初始化,如果完成就简单地返回;否则,pthread_once调用初始化函数,并且记录下初始化被完成。如果在一个线程初始时,另外的线程调用pthread_once,则调用线程等待,直到那个现成完成初始话返回。

    /*
     * once.c
     *
     * Demonstrate the use of pthread_once() one-time
     * initialization.
     */
    #include <pthread.h>
    #include "errors.h"
    
    pthread_once_t once_block = PTHREAD_ONCE_INIT;
    pthread_mutex_t mutex;
    
    /*
     * This is the one-time initialization routine. It will be
     * called exactly once, no matter how many calls to pthread_once
     * with the same control structure are made during the course of
     * the program.
     */
    void once_init_routine (void)
    {
        int status;
    
        status = pthread_mutex_init (&mutex, NULL);
        if (status != 0)
            err_abort (status, "Init Mutex");
    }
    
    /*
     * Thread start routine that calls pthread_once.
     */
    void *thread_routine (void *arg)
    {
        int status;
    
        status = pthread_once (&once_block, once_init_routine);
        if (status != 0)
            err_abort (status, "Once init");
        status = pthread_mutex_lock (&mutex);
        if (status != 0)
            err_abort (status, "Lock mutex");
        printf ("thread_routine has locked the mutex.\n");
        status = pthread_mutex_unlock (&mutex);
        if (status != 0)
            err_abort (status, "Unlock mutex");
        return NULL;
    }
    
    int main (int argc, char *argv[])
    {
        pthread_t thread_id;
        char *input, buffer[64];
        int status;
    
        status = pthread_create (&thread_id, NULL, thread_routine, NULL);
        if (status != 0)
            err_abort (status, "Create thread");
        status = pthread_once (&once_block, once_init_routine);
        if (status != 0)
            err_abort (status, "Once init");
        status = pthread_mutex_lock (&mutex);
        if (status != 0)
            err_abort (status, "Lock mutex");
        printf ("Main has locked the mutex.\n");
        status = pthread_mutex_unlock (&mutex);
        if (status != 0)
            err_abort (status, "Unlock mutex");
        status = pthread_join (thread_id, NULL);
        if (status != 0)
            err_abort (status, "Join thread");
        return 0;
    }
    


     

  • 相关阅读:
    jqueryeasyui1.2.3学习
    继续干IT的十个理由
    设计模式建造者模式(Builder Pattern)
    一段不错的视频
    第十一章:配置和安全IntelliMorph
    第十一章:配置和安全许可和配置
    物理库存和财务库存浅析
    第十一章:配置和安全概述
    IE6 双倍边距的解决方法
    asp实现关键词不区分大小写搜索并高亮显示
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6332843.html
Copyright © 2011-2022 走看看