线程函数(创建 取消 等待)

1.创建线程

创建线程的函数定义如下:

#include <pthread.h>

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

参数thread返回创建线程的ID 。attr用来设置线程的属性,一般置为NULL,参数start_routine是一个函数指针,指向一个函数 ,这个函数就是线程要运行的代码;arg参数 是start_routine指向的函数传入的参数。

用vi编辑器,终端输入命令:vi thread.c,输入命令如下:

#include <pthread.h>   //线程程序大多使用pthread库
#include <stdio.h>
#include <stdlib.h>

void* thread_func(void *arg)  //线程函数
{
 int *val=arg;
 printf("Hi,I'm a thread!/n");
 if(NULL!=arg)                        //如果参数不为空,打印参数内容
 printf("argument set:%d/n",*val);
}

int main()
{
 pthread_t tid;                       //线程ID
 int t_arg=100;                     //线程传入的参数值

 if(pthread_create(&tid, NULL,thread_func,&t_arg ))    //创建线程,创建成功返回0,失败则 返回错误号
 perror("Fail to create thread");

 sleep(1);                                   //睡眠1秒,等待线程执行
 printf("Main thread!/n");
return 0;

}

 

直接gcc thread.c会报错,因为thread库不是Linux的标准库,需要给编译器制定链接的库,使用gcc thread.c -lpthread命令后输入gcc thread.c仍报错:

/tmp/ccS2NySO.o: In function `main':
thread.c:(.text+0x67): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

解决办法 :gcc -o thread thread.c -lpthread
                   ./thread.c

得到输出如下

                 Hi,I'm a thread!
                 argument set:100
                 Main thread!

 

 

 

 

2.取消线程

函数定义如下:

#include <pthread.h>

int pthread_cancel(pthtead_t thread); //其中thread为线程ID

 

#include <pthread.h>   //线程程序大多使用pthread库
#include <stdio.h>
#include <stdlib.h>

void* thread_func(void *arg)  //线程函数
{
 int *val=arg;
 printf("Hi,I'm a thread!/n");
 if(NULL!=arg)                        //如果参数不为空,打印参数内容
  {
while(1)

printf("argument set:%d/n",*val);

}
}

int main()
{
 pthread_t tid;                       //线程ID
 int t_arg=100;                     //线程传入的参数值

 if(pthread_create(&tid, NULL,thread_func,&t_arg ))    //创建线程,创建成功返回0,失败则 返回错误号
 perror("Fail to create thread");

 sleep(1);                                   //睡眠1秒,等待线程执行
 printf("Main thread!/n");

 

pthread_cancel(tid);                  //取消线程
return 0;

}

运行结果如下:

                 Hi,I'm a thread!
                 argument set:100

                  (打印若干次参数值)
                 Main thread!


3.等待线程

在上面例子中,主线程使用sleep()函数暂停自己的运行,等待新创建的进程结束。但对于复杂点的程序延时函数就不好用了。

使用pthread_join()函数等待一个进程结束,函数定义如下:

#include <pthread.h>

int pthread_jion(pthread_t  thread,void **value_ptr);

参数thread是要等待线程的ID,参数value_ptr指向的是退出线程的返回值。如果被等待线程成功返回,函数返回0,其他其他情况返回出错代码。

 

 

4.多线程实例

在主程序中创建两个线程mid_thread和term_thread,mid线程不断等待term线程终止它,并且每隔2秒打印一次等待的次数。term接收从主函数传进来的mid线程的ID。如果线程ID合法,就调用pthread_cancel()函数结束mid线程。

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void* mid_thread(void *arg);  //mid线程声明
void* term_thread(void *arg); //term线程声明

int main()
{
pthread_t mid_tid,term_tid;   //存放线程ID
if(pthread_create(&mid_tid,NULL,mid_thread,NULL))   //创建mid线程
 {perror("Create mid thread error!");
 return 0;
}
if(pthread_create(&term_tid,NULL,term_thread,(void*)&mid_thread))   //创建term线程
{
 perror("Create term thread fail!/n");
return 0;
}
if(pthread_join(mid_tid,NULL))   //等待mid线程结束
{
 perror("wait mid thread error!");
return 0;

}
if(pthread_join(term_tid,NULL))  //等待term线程结束
{
 perror("wait term thread error!");
return 0;
}

return 0;
}
void* mid_thread(void *arg)  //mid线程定义
{
 int times=0;
 printf("mid thread create!/n");
 while(1)                           //不断打印等待的次数,间隔两秒
{
 printf("waiting term thread %d times!/n",times);
sleep(2);
times++;
}
}

void* term_thread(void *arg)   //term线程定义
{
 pthread_t *tid;
printf("term thread created!/n");
sleep(2);
if(NULL!=arg)
{
tid=(pthread_t*)arg;    //如果线程ID合法,结束线程
pthread_cancel((*tid));
}
}


"li.c" 56 lines, 897 characters
输出结果为:

mid thread create!
waiting term thread 0 times!
term thread created!
waiting term thread 1 times!

 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ANTRK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值