您的位置:首页 > 其它

线程的创建、等待及终止

2017-03-23 17:17 204 查看
首先来了解一下线程与进程的区别:

1.线程是在进程的地址空间中执行的控制流;

2.一个进程的多个线程共享同一地址空间;

3.进程是分配系统资源的基本单位;

4.线程是调度的基本单位;

5.在linux下,没有所谓的线程,线程都是用进程模拟出来的,线程又被称为轻量级进程。

注:进程拥有一个完整的虚拟地址空间,不依赖于线程而独立存在;反之,线程是进程的一部分,没有自己的地址空间,与进程内的其他线程一起共享分配给该进程的所有资源。

线程引用的pthread库,该库由POSIX标准定义,所以在编译时需引入该库。gcc -o test test.c -lpthread

一.线程的创建:

int pthread_create(pthread_t thread, const pthread_attr_t *attr, void (start_routine) (void ), void *arg);

创建成功:返回0;

创建失败:返回错误码,该错误码不保存在perror()中,所以可将错误码通过strerror()转换为字符串描述。

参数1: pthread_t *thread:线程id地址(只在用户区有效)。

参数2:const pthread_attr_t *attr:线程属性,一般设置为NULL,因为没有人比操作系统更为了解该线程。

参数3: void (*start_routine) (void ):函数指针,指向线程函数。

参数4:void *arg:线程函数的参数。

#include<stdio.h>
#include<pthread.h>
void* thread_run(void* _val)//线程函数
{
//获取进程id:getpid();获取线程id:pthread_self()
printf("%s: pid:%d,tid:%lu\n",(char*)_val,(int)getpid(),(unsigned long long)pthread_self());
return (void *)0;
}
int main()
{
pthread_t tid;
int ret = pthread_create(&tid,NULL,thread_run,"other thread run");//创建线程
if(ret!=0)
{
printf("pthread create failed! error info:%s\n",strerror(ret));
return -1;
}
printf("main thread run:  pid:%d,tid:%lu\n",(int)getpid(),(unsigned long long)pthread_self());
void *val;
pthread_join(tid,&val);//线程等待
printf("thread is quit,thread id is %lu,exit code:%d\n",(unsigned long long)tid,(int)val);
return 0;
}




二.线程的等待及终止:

线程等待:

int pthread_join(pthread_t thread, void **retval)

创建成功:返回0;

创建失败:返回错误码;

参数1: pthread_t thread:线程id;

参数2:void **retval:一个二级指针,从系统中获取线程退出信息。(接收线程终止时的退出码)

线程终止(三种方式):

方法一:线程函数中 return,此时return的是void*类型。

方法二:线程调用pthread_exit(void* retval);retval指针保存退出码。

方法三:线程可被取消,调用int pthread_cancel(pthread_t thread)

pthread_t thread:线程id;

(1)线程被自己cancel,退出码为0;

(2)线程别其他线程cancel,退出码为-1。

注:不要使用exit来退出线程,因为线程属于进程的一部分,不管在哪调用exit,都相当于是进程退出,进程退出了,线程就不会存在了(exit是用来退出进程的)。

程序示例:

#include<stdio.h>
#include<pthread.h>
void* thread1(void* _val)//quit by return
{
int count = 3;
while(count--)
{
printf("i am thread1\n");
}
return (void*)121;
}
void* thread2(void* _val)//quit by pthread_exit()
{
int count = 3;
while(count--)
{
printf("i am thread2\n");
}
pthread_exit((void*)122);
}
void* thread3(void* _val)//quit by main thread---pthread_cancel()
{
int count = 3;
while(count--)
{
printf("i am thread3\n");
}
}
void* thread4(void* _val)//quit by itself----pthread_cancel()
{
int count = 3;
while(count--)
{
printf("i am thread4\n");
}
pthread_cancel(pthread_self());
}
int main()
{
pthread_t tid1,tid2,tid3,tid4;
void* status=NULL;
pthread_create(&tid1,NULL,thread1,NULL);
pthread_create(&tid2,NULL,thread2,NULL);
pthread_create(&tid3,NULL,thread3,NULL);
pthread_create(&tid4,NULL,thread4,NULL);
pthread_cancel(tid3);
pthread_join(tid1,&status);
printf("thread1 is quit,thread1 id is %lu,exit code:%d\n",tid1,(int)status);
pthread_join(tid2,&status);
printf("thread2 is quit,thread2 id is %lu,exit code:%d\n",tid2,(int)status);
pthread_join(tid3,&status);
printf("thread3 is quit,thread3 id is %lu,exit code:%d\n",tid3,(int)status);
pthread_join(tid4,&status);
printf("thread4 is quit,thread4 id is %lu,exit code:%d\n",tid4,(int)status);
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: