创建线程
int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void *arg);)
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。(通常为NULL)
第三个参数是线程运行函数的起始地址。
最后一个参数是运行函数的参数
结束线程
void pthread_exit(void* retval);
线程结束后与进程归并到一起
int pthread_join(pthread_t thread, void **retval);
取消线程
int pthread_cancel(pthread_t thread)
int pthread_setcancelstate(int state, int *oldstate)
int pthread_setcanceltype(int type, int *oldtype)
pthread_create(),pthread_join(),pthread_exit()函数的代码实现
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
void *thread_function(void *arg);
char message[]="THREAD_TEST";
int main()
{
int res; //保存线程的返回值
pthread_t a_thread; //保存线程标识符等信息
void *thread_result;//结收线程结束时的返回值
res=pthread_create(&a_thread,NULL,thread_function,(void *) message);
if(res!=0)
{
perror("创建线程失败");
exit(EXIT_FAILURE);
}
printf("等待线程等待。。。。。");
res=pthread_join(a_thread,&thread_result);
if(res!=0)
{
perror("等待线程创建失败");
exit(EXIT_FAILURE);
}
printf("线程已经结束,返回值::%s\n",(char *)thread_result);
printf("message 的值为::%s\n",message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("线程在运行,参数为:%s\n",(char*) arg);
sleep(3);
strcpy(message,"线程已经修改");
pthread_exit("线程执行完毕");
}
取消线程的代码实现
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg);
int main ()
{
int res;
pthread_t a_thread;
void * thread_result;
res=pthread_create(&a_thread,NULL,thread_function,NULL);
if(res!=0)
{
perror("线程创建失败");
exit(EXIT_FAILURE);
}
sleep(3);
printf("取消线程......\n");
res=pthread_cancel(a_thread); //发出取消线程请求
if(res!=0)
{
perror("线程取消失败");
exit(EXIT_FAILURE);
}
printf("等待线程结束......\n");
res=pthread_join(a_thread,thread_result);
if(res!=0)
{
perror("线程结束失败");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
int i,res;
res=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);//定义线程状态 允许接受取消请求
if(res!=0)
{
perror("定义线程状态失败");
exit(EXIT_FAILURE);
}
res=pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);//定义线程结束的方式为采取一些动作后再结束
if(res!=0)
{
perror("定义线程结束失败");
exit(EXIT_FAILURE);
}
printf("线程函数正在运行\n");
for(i=0;i<10;i++)
{
printf("正在运行。。。。。。%d",i);
sleep(1);
}
pthread_exit(0);
}
多线程的代码实现
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 6
void *thread_function(void * arg);
int main ()
{
int res;
pthread_t a_thread[NUM_THREADS];
void *thread_result;
int lost_of_threads;
for (lost_of_threads=0;lost_of_threads<NUM_THREADS;lost_of_threads++)
{
res=pthread_create(&(a_thread[lost_of_threads]),NULL,thread_function,(void *) &lost_of_threads);
if(res!=0)
{
perror("创建失败");
exit(EXIT_FAILURE);
}
sleep(1);
}
printf("等待线程结束.....\n");
for(lost_of_threads=NUM_THREADS-1;lost_of_threads>=0;lost_of_threads--)
{
res=pthread_join(a_thread[lost_of_threads],&thread_result);
if(res!=0)
{
perror("线程结束失败\n");
}
else {
printf("结束一个线程\n");
}
}
printf("线程全部结束\n");
exit(EXIT_SUCCESS);
}
void *thread_function(void * arg)
{
int my_number=*(int *)arg;//接收主线程传递的参数
int rand_num;
printf("线程已经运行 参数为:%d\n",my_number);
rand_num=1+(int) (9.0*rand()/(RAND_MAX+1.0));
sleep(rand_num);
printf("第%d个线程结束\n",my_number);
pthread_exit(NULL);
}