[linux basic]--线程

时间:2021-07-10 16:01:18
/*************************************************************************
> 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 != ){
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!=){
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();
//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 = ; 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 = ;
while(print_count1++ < ){
if(run_now == ){
printf("");
run_now = ;
}else{
sleep();
}
} if(res != ){
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!=){
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 = ;
while(print_count2++ < ){
if(run_now == ){
printf("");
run_now = ;
}else{
sleep();
}
}
printf("\n");
}

执行结果:

lizhen@lizhen:~/basic$ ./thread2

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

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

同步