美文网首页
19.线程的创建终止汇合及分离

19.线程的创建终止汇合及分离

作者: 陈忠俊 | 来源:发表于2020-05-04 16:26 被阅读0次

1.创建新的线程

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void *doit(void *arg){
    printf("arg: %s\tpid: %d\ttid:%lu\n", (char *)arg, getpid(), pthread_self());
    return NULL;
}

int main(void){
    pthread_t tid;
    //create a new thread;
    pthread_create(&tid, NULL, doit, "New-thread");
    //we have 2 threads now, main and new created thread;
    //the 2 threads running asynchronous
    sleep(1);
    doit("main");

    return 0;
}

编译运行:

zhongjun@Eclipse:~$ gcc pthread.c -lpthread
zhongjun@Eclipse:~$ ./a.out
arg: New-thread pid: 227        tid:140124534671104
arg: main       pid: 227        tid:140124545353536

2.线程的分离

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void *doit(void *arg){
    for(int i = 0; i < 5; i++){
        printf("arg: %s\tpid: %d\ttid:%lu\n", (char *)arg, getpid(), pthread_self());
        sleep(4);
    }
    return NULL;
}

int main(void){
    pthread_t tid;
    //create a new thread;
    pthread_create(&tid, NULL, doit, "New-thread");
    //detach new thread
    pthread_detach(tid);
    for(int i = 0; i < 5; i++){
        printf("pid: %d\ttid: %lu\n", getpid(), pthread_self());
        sleep(5);
    }

    return 0;
}

编译运行后,通过命令top -H -p "thread_id"查看效果。

3.线程的汇合和终止

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void *handler(void *arg){
    printf("I am running, handler...\n");
    sleep(5);
    return (void *)1;
}

void *handler_cancel(void *arg){
    printf("Running thread cancel...\n");
    while(1){
        printf("cancel...\n");
        sleep(2);
    }

    return NULL;
}
void *handler_exit(void *arg){
    printf("handler-exit running...\n");
    sleep(5);
    pthread_exit((void *)5);
}
int main(void){
    void *ret;
    pthread_t tid;
    pthread_create(&tid, NULL, handler, NULL);
    //join thread, blocking mode
    pthread_join(tid, &ret);
    printf("exit now...%d\n", (int)ret);
    
    //pthread_exit 
    pthread_create(&tid, NULL, handler_exit, NULL);
    pthread_join(tid, &ret);
    printf("Handler_exit code is: %d\n", (int)ret);

    //thread cancel...
    sleep(3);
    pthread_create(&tid, NULL, handler_cancel, NULL);
    pthread_cancel(tid);
    pthread_join(tid, &ret);
    if(ret == PTHREAD_CANCELED)
        printf("handler func exit code is: %d\n", (int)ret);
    return 0;
}

相关文章

  • 19.线程的创建终止汇合及分离

    1.创建新的线程 编译运行: 2.线程的分离 编译运行后,通过命令top -H -p "thread_id"查看效...

  • 多线程编程

    摘要 线程概念,线程与进程的区别与联系学会线程控制,线程创建,线程终止,线程等待了解线程分离与线程安全学会线程同步...

  • Linux下多线程的使用

    一、线程的创建 1.创建分离线程方法一:(常用) 方法二: 2.创建非分离线程 二、线程的使用 三、分离线程与非分...

  • Thread的使用二

    在Thread的使用一中,我们讲解了线程的创建,使用及线程池,这节我们来看看当线程使用完之后,我们改如何终止线程。...

  • 一文搞懂 Java 线程中断

    在之前的一文《如何"优雅"地终止一个线程》中详细说明了 stop 终止线程的坏处及如何优雅地终止线程,那么还有别的...

  • 【代码】Java多线程实现

    一、什么情况下创建线程不使用线程池? 1、需要定义线程的优先级; 2、需要创建前台线程; 3、需要手动终止线程; ...

  • 本地文件队列-异步隔离设计

    常见的异步方式: 创建异步线程 每个新创建一个线程来执行异步任务,任务结束线程也终止。线程的创建成本比较大,不建议...

  • 多线程之NSThread

    开启线程 分离主线程创建:创建线程后会自动执行,但是线程外部不可获取到该线程对象detachNewThreadWi...

  • Win32多线程处理技术【线程的创建】

    线程的创建 线程内核对象 线程的终止 线程的优先级 Windows程序的执行单元 多线程 进程---主线程---辅...

  • Pthread

    线程的join和detach两个状态 当一个可汇合的线程终止时,它的线程ID和退出状态将留到另一个线程对它调用pt...

网友评论

      本文标题:19.线程的创建终止汇合及分离

      本文链接:https://www.haomeiwen.com/subject/lsqyghtx.html