练习

来源:互联网 发布:ios10怎么清除游戏数据 编辑:程序博客网 时间:2024/05/16 18:23
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

char message[]="hello world!\n";

void * thread_function(void * arg)
{
    printf("thread_function is running! argument is %s\n",(char *)arg);
    sleep(3);
    strcpy(message,"bye");
    pthread_exit("thank for your CPU time!\n\a");
}

int main()
{
    int res;
    pthread_t a_thread;
    void * thread_result;

    res = pthread_create(&a_thread,NULL,thread_function,(void *)message);
    printf("waiting for thread to finish!\n");
    res = pthread_join(a_thread,&thread_result);
    printf("thread joined, it returned %s\n",(char *)thread_result);
    printf("message is now %s\n",message);

    return 0;
}
0 0
原创粉丝点击