Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • linux 线程学习

    post by onelong / 2013-7-29 3:56 Monday [linux]

    /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

    /*

     * main.c

     * Copyright (C) 2013 Onelong

     * 

     * PthreadTest is free software: you can redistribute it and/or modify it

     * under the terms of the GNU General Public License as published by the

     * Free Software Foundation, either version 3 of the License, or

     * (at your option) any later version.

     * 

     * PthreadTest is distributed in the hope that it will be useful, but

     * WITHOUT ANY WARRANTY; without even the implied warranty of

     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

     * See the GNU General Public License for more details.

     * 

     * You should have received a copy of the GNU General Public License along

     * with this program.  If not, see <http://www.gnu.org/licenses/>.

     * 

     * int pthread_setspecific(pthread_key_t key, const void *value);

       void *pthread_getspecific(pthread_key_t key);

       int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));

     */

     

    #include <stdio.h>

    #include <stdlib.h>

    #include <pthread.h>

    #include <time.h> 

    //<sys.time.h>

     

     

    static pthread_t mThread,mThread1;

    static pthread_barrier_t barrier; //线程屏障

    static pthread_mutex_t mtx;// = PTHREAD_MUTEX_INITIALIZER; //互斥锁

    static pthread_cond_t cond;// = PTHREAD_COND_INITIALIZER; //条件

    static pthread_key_t key; //线程局部变量

     

    void destroy_key(void * data) //析构函数

    {

    char* d = (char*)data;

    printf ("destroy %s \n",d);

    }

     

    void getData() //读取线程局部变量

    {  

        char* data = (char*) pthread_getspecific(key);  

        printf ("%s\n", data);

    }  

     

    void *run(void * arg)

    {

    printf ("hello world!\n");

     

    char* data = "run....";

    pthread_setspecific(key, data); //设置一个局部变量

     

    pthread_barrier_wait(&barrier); //设置一个线程屏障

    printf ("run after barrier....\n");

    getData();

    return NULL;

    }

     

    void *run1(void * arg)

    {

    printf ("hello world!\n");

    char* data = "run1....";

    pthread_setspecific(key, data); 

     

    struct timespec tv;

    tv.tv_sec = time(NULL);

    tv.tv_sec += 5;

    int ret = pthread_cond_timedwait(&cond, &mtx, &tv); //条件等待

    printf ("delay %d\n",ret);

     

    //pthread_delay_np(&delay); //can't use in linux 

    //sleep(5); //warning 

    pthread_barrier_wait(&barrier);

    printf ("run1 after barrier....\n");

    getData();

    return NULL;

    }

     

     

    int main()

    {

     

    pthread_barrier_init(&barrier,NULL,2);

    pthread_mutex_init(&mtx,NULL);

    pthread_cond_init(&cond,NULL);

     

    pthread_key_create(&key,destroy_key);

     

    pthread_create(&mThread,NULL,run,NULL);

    pthread_create(&mThread1,NULL,run1,NULL);

    pthread_join(mThread,NULL);

    pthread_join(mThread1,NULL);

     

    pthread_barrier_destroy(&barrier);

    pthread_mutex_destroy(&mtx);

    pthread_cond_destroy(&cond);

     

        //pthread_exit(&mThread);

     

     

    return (0);

    }

    更多的 http://blog.csdn.net/monkey_d_meng/article/details/5628663

    标签: linux
    引用地址:
     

    我要评论