线程创建、线程等待、线程终止
2016-12-02 21:16
218 查看
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
char message[]="hello world!";
void * thread_function(void *arg)
{
printf("thread function is running, argument is %s\n",(char *)arg);
sleep(3);
strcpy(message,"bye");
pthread_exit("thank you for your CPU time!");
}
int main()
{
pthread_t a_thread;
void * thread_result;
int ret;
ret = pthread_create(&a_thread,NULL,(void *)thread_function,(void *)message);
if(ret != 0)
{
perror("create thread error");
exit(0);
}
printf("waiting for thread to finish...\n");
ret = pthread_join(a_thread,&thread_result);
if(ret != 0)
{
perror("thread join error");
exit(0);
}
printf("thread joined,it returned %s\n",(char*)thread_result);
printf("message is now %s\n",message);
return 0;
}
#include <pthread.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
char message[]="hello world!";
void * thread_function(void *arg)
{
printf("thread function is running, argument is %s\n",(char *)arg);
sleep(3);
strcpy(message,"bye");
pthread_exit("thank you for your CPU time!");
}
int main()
{
pthread_t a_thread;
void * thread_result;
int ret;
ret = pthread_create(&a_thread,NULL,(void *)thread_function,(void *)message);
if(ret != 0)
{
perror("create thread error");
exit(0);
}
printf("waiting for thread to finish...\n");
ret = pthread_join(a_thread,&thread_result);
if(ret != 0)
{
perror("thread join error");
exit(0);
}
printf("thread joined,it returned %s\n",(char*)thread_result);
printf("message is now %s\n",message);
return 0;
}
相关文章推荐
- Linux 线程(创建/等待/终止)
- 线程的创建、等待与终止
- Linux 线程(创建/等待/终止)
- JAVA笔记13__创建线程/线程休眠/等待线程终止/线程中断/守护线程
- [置顶] 【Linux】线程总结:初识、创建、等待、终止、分离
- 线程的创建、等待、终止、分离
- Linux之线程的创建、等待、终止及分离
- 线程的创建,等待与终止
- 线程的控制(创建、等待、终止)、分离线程
- 线程创建、等待、终止
- 【线程】多线程创建,传参,共享数据,终止,等待,清除
- 线程的创建、终止、等待
- linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结
- 线程的创建、等待及终止
- Linux-线程(一)创建 终止 等待线程
- 线程的创建、等待、终止以及属性
- 【Linux】线程(概念、特点、线程控制代码--创建/等待/终止、分离与结合属性)
- 简单的,线程创建,线程等待,线程终止。
- 线程的创建、终止、等待
- uc笔记11---线程管理,线程函数:创建、等待、获取、比较、终止、取消, 线程属性