美文网首页
Linux-C-Day6-线程

Linux-C-Day6-线程

作者: 国服最坑开发 | 来源:发表于2023-02-26 18:59 被阅读0次

1.子线程创建

  • pthread_t
  • pthread_create
  • pthread_self
//
// Created by gorey on 2023/02/27.
//
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <pthread.h>

pthread_t sub_tid;

void print_tds(const char *s) {
    pid_t pid;
    pthread_t tid;
    pid = getpid();
    tid = pthread_self();
    printf("%s pid: %u, tid: %u (0x%x)\n", s, (unsigned int) pid, (unsigned int) tid, (unsigned int) tid);
}

/**
 * 子线程处理内容
 */
void *thr_fn(void *arg) {
    print_tds(arg);
    return NULL;
}


int main(int argc, char *argv[]) {
    int err;
    err = pthread_create(&sub_tid, NULL, thr_fn, "new thread: "); // 创建子线程,通过函数传入子线程处理内容
    if (err != 0) {
        fprintf(stderr, "can't create thread: %s\n", strerror(err));
        exit(1);
    }
    printf("sub thread id: %u\n", sub_tid);
    print_tds("main thread:");

    sleep(1);

    return 0;
}

2.线程同步

  • pthread_create
  • pthread_join
  • pthread_cancel
  • sleep
//
// Created by gorey on 2023/02/27.
//
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <pthread.h>


void *thr_fn1(void *arg) {
    printf("thread 1 returning \n");
    sleep(3);
    return (void *) 1;
}

void *thr_fn2(void *arg) {
    printf("thread 2 returning \n");
    return (void *) (2 + (long) arg);
}

void *thr_fn3(void *arg) {
    while(1) {
        printf("thread 3 writing\n");
        sleep(1);
    }
}


/**
 * 线程三种退出方法
 * 1.子线程 return
 * 2.子线程调用 pthread_cancel 结束另一个同进程中的线程。分同步和异步两种场景考虑
 * 3.线程调用本线程的 pthread_exit
 */
int main(int argc, char *argv[]) {
    pthread_t tid;
    void *tret;

    pthread_create(&tid, NULL, thr_fn1, NULL);
    pthread_join(tid, &tret); // 主线程 等待子线程结束。 正常结束时,tret 保存的是 子线程方法返回值。
    printf("thread 1 exit code %ld\n", (long) tret);

    pthread_create(&tid, NULL, thr_fn2, (void *) 100); // 第4个参数为 子线程函数的参数
    pthread_join(tid, &tret);
    printf("thread 2 exit code %ld\n", (long) tret);

    pthread_create(&tid, NULL, thr_fn3, NULL);
    sleep(3);
    pthread_cancel(tid); // 让子线程结束
    pthread_join(tid, &tret);  //  此时再去同步等待子线程, tret的结果是:PTHREAD_CANCELED (-1)
    printf("thread 3 exit code %ld\n", (long) tret);

    return 0;
}

输出:

thread 1 returning 
thread 1 exit code 1
thread 2 returning 
thread 2 exit code 102
thread 3 writing
thread 3 writing
thread 3 writing
thread 3 writing
thread 3 exit code 1

相关文章

  • Android

    线程间通信 主线程和工作线程主线程和工作线程 工作线程与工作线程工作线程与工作线程 为什么主线程Looper.lo...

  • 三、操作系统之线程

    前言 什么是线程 引入线程的原因 线程的概念 线程和进程的关系 线程结构 线程有点 多线程模型 用户线程和内核线程...

  • Thread

    队列 线程锁 多线程,线程池 队列 多线程爬虫示例 多线程 自定义线程 线程池

  • 总结多线程与设计模式+synchronized+性能+高吞吐+死

    Java线程 Java语言的线程 何谓线程 线程启动 线程的暂时停止 线程的共享互斥 线程的协调 线程的状态转移 ...

  • 多线程编程

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

  • java线程池

    线程VS线程池 普通线程使用 创建线程池 执行任务 执行完毕,释放线程对象 线程池 创建线程池 拿线程池线程去执行...

  • java并发之守护线程

    java中有两种线程,用户线程和守护线程用户线程:主线程停止时,用户线程不会停止守护线程:主线程停止时,守护线程也...

  • Java线程池的使用

    线程类型: 固定线程 cached线程 定时线程 固定线程池使用 cache线程池使用 定时调度线程池使用

  • 线程基础知识

    线程学习 线程的基础知识 线程是什么? 线程和进程的关系 线程的6个状态 线程优先级 主线程、多线程、后台线程的概...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

网友评论

      本文标题:Linux-C-Day6-线程

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