码迷,mamicode.com
首页 > 编程语言 > 详细

[linux basic]--线程

时间:2016-03-26 23:52:19      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

/*************************************************************************
    > File Name: thread1.c
    > Author: 
    > Mail: 
    > Created Time: 2016年03月26日 星期六 22时37分44秒
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>

void *thread_function(void *arg);
char message[] = "hello world";

int main(){
    int res;
    pthread_t a_thread;
    void *thread_result;
    //pthread_create 开始运行多线程,如下所示
    res = pthread_create(&a_thread,NULL,thread_function,(void*)message);
    //先向pthrea_create传递了一个pthread_t类型对象的地址
    //后面我们可以用它来引用这个新线程
    //不想改变默认线程属性,第二个参数设置为NULL
    //最后两个参数分别是将要调用的函数和一个传递给该函数的参数

    if(res != 0){
        perror("thread creation failed");
        exit(EXIT_FAILURE);
    }
    //如果成功,就有两个线程运行,
    //1原来的线程,main继续执行pthread_create后面的代码
    //2新线程开始执行thread_function函数
    printf("Waiting for thread to finsh...\n");

    res = pthread_join(a_thread, &thread_result);
    //a_thread 正在等待其结束的线程的标识符
    //thread_result,指向线程返回值的指针
    //这个函数将等到它所指定的线程终止才返回,想想wait()的功能
    if(res!=0){
        perror("thread join failed");
        exit(EXIT_FAILURE);
    }
    //然后main打印新线程的返回值和全局变量message的值
    //exit
    printf("thread join, it returned %s\n",(char*)thread_result);
    printf("Message is now %s\n",message);
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg){
    printf("thread_function is running. Argument was %s\n",(char*)arg);
    sleep(3);
    //todo something
    strcpy(message,"Bye!");
    pthread_exit("thank you for the cpu time");
}

执行结果

lizhen@lizhen:~/basic$ ./thread1 
Waiting for thread to finsh...
thread_function is running. Argument was hello world
thread join, it returned thank you for the cpu time
Message is now Bye!

 ======================

同时执行--

/*************************************************************************
    > File Name: thread1.c
    > Author: 
    > Mail: 
    > Created Time: 2016年03月26日 星期六 22时37分44秒
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>

void *thread_function(void *arg);
char message[] = "hello world";
int run_now = 1;

int main(){
    int res;
    pthread_t a_thread;
    void *thread_result;
    //pthread_create 开始运行多线程,如下所示
    res = pthread_create(&a_thread,NULL,thread_function,(void*)message );
    //先向pthrea_create传递了一个pthread_t类型对象的地址
    //后面我们可以用它来引用这个新线程
    //不想改变默认线程属性,第二个参数设置为NULL
    //最后两个参数分别是将要调用的函数和一个传递给该函数的参数
    int print_count1 = 0;
    while(print_count1++ < 20){
        if(run_now == 1){
            printf("1");
            run_now = 2;
        }else{
            sleep(1);
        }
    }

    if(res != 0){
        perror("thread creation failed");
        exit(EXIT_FAILURE);
    }
    //如果成功,就有两个线程运行,
    //1原来的线程,main继续执行pthread_create后面的代码
    //2新线程开始执行thread_function函数
    printf("Waiting for thread to finsh...\n");

    res = pthread_join(a_thread, &thread_result);
    //a_thread 正在等待其结束的线程的标识符
    //thread_result,指向线程返回值的指针
    //这个函数将等到它所指定的线程终止才返回,想想wait()的功能
    printf("thread joined\n");
    if(res!=0){
        perror("thread join failed");
        exit(EXIT_FAILURE);
    }
    //然后main打印新线程的返回值和全局变量message的值
    //exit
    //printf("thread join, it returned %s\n",(char*)thread_result);
//    printf("Message is now %s\n",message);
    printf("\n");
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg){
    //todo something
    int print_count2 = 0;
    while(print_count2++ < 20){
        if(run_now == 2){
            printf("2");
            run_now = 1;
        }else{
            sleep(1);
        }
    }
    printf("\n");
}

执行结果:

lizhen@lizhen:~/basic$ ./thread2
12121212121212121212
Waiting for thread to finsh...
thread joined

lizhen@lizhen:~/basic$ 

============

同步

 

[linux basic]--线程

标签:

原文地址:http://www.cnblogs.com/li-daphne/p/5324461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!