创建线程,结束线程,取消线程,以及多线程的实现

创建线程

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)

设置本线程对Cancel信号的反应,state有两种值:PTHREAD_CANCEL_ENABLE(缺省)和 PTHREAD_CANCEL_DISABLE,分别表示收到信号后设为CANCLED状态和忽略CANCEL信号继续运行;old_state如果不为 NULL则存入原来的Cancel状态以便恢复。

int pthread_setcanceltype(int type, int *oldtype)

设置本线程取消动作的执行时机,type由两种取值:PTHREAD_CANCEL_DEFERRED和 PTHREAD_CANCEL_ASYNCHRONOUS,仅当Cancel状态为Enable时有效,分别表示收到信号后继续运行至下一个取消点再退出和 立即执行取消动作(退出);oldtype如果不为NULL则存入原来的取消动作类型值。

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);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值