概述

每个进程都拥有自己的数据段、代码段和堆栈段,这就造成进程在进行创建、切换、撤销操作时,需要较大的系统开销。为了减少系统开销,从进程中演化出了线程。为了让进程完成一定的工作,进程必须至少包含一个线程。线程存在于进程中,共享进程的资源。更多详情,请看《进程和线程的区别与联系》。


就像每个进程都有一个进程号一样,每个线程也有一个线程号。进程号在整个系统中是唯一的,但线程号不同,线程号只在它所属的进程环境中有效。进程号用 pid_t 数据类型表示,是一个非负整数。线程号则用 pthread_t 数据类型来表示,Linux 使用无符号长整数表示。有的系统在实现 pthread_t 的时候,用一个结构体来表示,所以在可移植的操作系统实现不能把它做为整数处理。


线程的常用函数

1)获取线程号

所需头文件:


#include <pthread.h>



pthread_t pthread_self(void);

功能:


获取线程号。


参数:



返回值:


调用线程的线程 ID 。



2)线程号的比较


所需头文件:


#include <pthread.h>



int pthread_equal(pthread_t t1, pthread_t t2);


功能:


判断线程号 t1 和 t2 是否相等。为了方便移植,尽量使用函数来比较线程 ID。


参数:


t1,t2​:待判断的线程号。


返回值:


相等:  非 0

不相等:0



示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int main(int argc, char *argv[])
{
pthread_t thread_id;

thread_id = pthread_self(); // 返回调用线程的线程ID
printf("Thread ID = %lu \n",thread_id);

if( 0 != pthread_equal( thread_id, pthread_self() ) ){
printf("Equal!\n");
}else{
printf("Not equal!\n");
}

return 0;
}


线程函数的程序在 pthread 库中,故链接时要加上参数 -lpthread​。





运行结果如下:

【Linux系统编程】线程的基本操作_示例代码


3)线程的创建

所需头文件:


#include <pthread.h>



int pthread_create( pthread_t *thread,


const pthread_attr_t *attr,


void *(*start_routine)(void *),


void *arg );


功能:


创建一个线程。


参数:


thread​:线程标识符地址。

attr​:线程属性结构体地址,通常设置为 NULL。

start_routine​:线程函数的入口地址。

arg​:传给线程函数的参数。


返回值:


成功:0

失败:非 0



pthread_create() 创建的线程从指定的回调函数开始运行,该函数运行完后,该线程也就退出了。线程依赖进程存在的,共享进程的资源,如果创建线程的进程结束了,线程也就结束了。


示例一:


#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int var = 8;

void *thread_1(void *arg)
{
while(1)
{
printf("this is my new thread1: var++\n");
var++;
sleep(1);
}
return NULL;
}

void *thread_2(void * arg)
{
while(1){
printf("this is my new thread2: var = %d\n", var);
sleep(1);
}

return NULL;
}

int main(int argc, char *argv[])
{
pthread_t tid1,tid2;

//创建两个线程
pthread_create(&tid1, NULL, thread_1, NULL);
pthread_create(&tid2, NULL, thread_2, NULL);

while(1){
printf("the main thread: var = %d\n", var);
sleep(1);
}

return 0;
}







运行结果如下:


【Linux系统编程】线程的基本操作_#include_02


示例二:


#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

// 回调函数
void *thread_fun(void * arg)
{
sleep(1);
int num = *( (int *)arg );
printf("int the new thread: num = %d\n", num);

return NULL;
}

int main(int argc, char *argv[])
{
pthread_t tid;
int test = 100;

// 创建线程, 把 &test 传给回调函数 thread_fun()
pthread_create(&tid, NULL, thread_fun, (void *)&test);

while(1);

return 0;
}







运行结果如下:


【Linux系统编程】线程的基本操作_头文件_03


4)回收线程资源

所需头文件:


#include <pthread.h>



int pthread_join(pthread_t thread, void **retval);

功能:


等待线程结束(此函数会阻塞),并回收线程资源,类似进程的 wait() 函数。如果线程已经结束,那么该函数会立即返回。


参数:


thread​:被等待的线程号。
retval​:用来存储线程退出状态的指针的地址。


返回值:


成功:0

失败:非 0



示例代码如下:


#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void *thead(void *arg)
{
static int num = 123; //静态变量

printf("after 2 seceonds, thread will return\n");
sleep(2);

return #
}

int main(int argc, char *argv[])
{
pthread_t tid;
int ret = 0;
void *value = NULL;

// 创建线程
ret = pthread_create(&tid, NULL, thead, NULL);
if(ret != 0){ //创建失败
perror("pthread_create");
}

// 等待线程号为 tid 的线程,如果此线程结束就回收其资源
// &value保存线程退出的返回值
pthread_join(tid, &value);

printf("value = %d\n", *( (int *)value ) );

return 0;
}







运行结果如下:


【Linux系统编程】线程的基本操作_示例代码_04


创建一个线程后应回收其资源,但使用 pthread_join() 函数会使调用者阻塞,Linux 还提供了非阻塞函数 pthread_detach() 来回收线程的资源。


所需头文件:


#include <pthread.h>



int pthread_detach(pthread_t thread);

功能:


使调用线程与当前进程分离,分离后不代表此线程不依赖与当前进程,​线程分离的目的​是将线程资源的回收工作交由系统自动来完成,也就是说当被分离的线程结束之后,系统会自动回收它的资源。所以,此函数不会阻塞。


参数:


thread​:线程号。


返回值:


成功:0



失败:非 0



注意,调用 pthread_detach() 后再调用 pthread_join() , pthread_join() 会立马返回,调用失败。


示例代码如下:


#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void *thead(void *arg)
{
int i;
for(i=0; i<5; i++)
{
printf("I am runing\n");
sleep(1);
}

return NULL;
}

int main(int argc, char *argv[])
{
int ret = 0;
pthread_t tid;

ret = pthread_create(&tid, NULL, thead, NULL);
if(ret!=0){
perror("pthread_create");
}

pthread_detach(tid); // 线程分离,不阻塞

// 立马返回,调用失败
int flag = pthread_join(tid, NULL);
if(flag != 0){
printf("join not working\n");
}

printf("after join\n");
sleep(3);
printf("I am leaving\n");

return 0;
}







运行结果如下:


【Linux系统编程】线程的基本操作_示例代码_05


5)线程退出

在进程中我们可以调用 exit() 函数或 _exit() 函数来结束进程,在一个线程中我们可以通过 pthread_exit() 在不终止整个进程的情况下停止它的控制流。


所需头文件:


#include <pthread.h>



void pthread_exit(void *retval);

功能:


退出调用线程。一个进程中的多个线程是共享该进程的数据段,因此,通常线程退出后所占用的资源并不会释放。


参数:


retval​:存储线程退出状态的指针。


返回值:




示例代码如下:


#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void *thread(void *arg)
{
static int num = 123; //静态变量
int i = 0;
while(1)
{
printf("I am runing\n");
sleep(1);
i++;
if(i==3)
{
pthread_exit( (void *)&num );
// return #
}
}

return NULL;
}

int main(int argc, char *argv[])
{
int ret = 0;
pthread_t tid;
void *value = NULL;

ret = pthread_create(&tid, NULL, thread, NULL);
if(ret!=0){
perror("pthread_create");
}

pthread_join(tid, &value);

printf("value = %d\n", *(int *)value );

return 0;
}








运行结果如下:

【Linux系统编程】线程的基本操作_示例代码_06