zoukankan      html  css  js  c++  java
  • C语言多线程的一个简单例子

      多线程的一个简单例子:

      

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <pthread.h>
    
    
    void * print_a(void *);
    void * print_b(void *);
    
    int main(){
    
        pthread_t t0;
        pthread_t t1;
    
        // 创建线程A
        if(pthread_create(&t0, NULL, print_a, NULL) == -1){
            puts("fail to create pthread t0");
            exit(1);
        }
    
        if(pthread_create(&t1, NULL, print_b, NULL) == -1){
            puts("fail to create pthread t1");
            exit(1);
        }
    
        // 等待线程结束
        void * result;
        if(pthread_join(t0, &result) == -1){
            puts("fail to recollect t0");
            exit(1);
        }
    
        if(pthread_join(t1, &result) == -1){
            puts("fail to recollect t1");
            exit(1);
        }
    
    
        return 0;
    }
    
    
    // 线程A 方法
    void * print_a(void *a){
        for(int i = 0;i < 10; i++){
            sleep(1);
            puts("aa");
        }
        return NULL;
    
    }
    
    // 线程B 方法
    void * print_b(void *b){
        for(int i=0;i<20;i++){
            sleep(1);
            puts("bb");
        }
        return NULL;
    }

    打印:

    aa
    bb
    aa
    aa
    bb
    ...
  • 相关阅读:
    C++ 字符数组
    C++ 从函数返回数组
    C++给函数传数组参数
    串行通信的三种方式
    进程间通信pipe和fifo
    嵌入式开发基本知识
    查找算法
    排序算法
    offsetof与container_of宏[总结]
    uboot自定义添加命令
  • 原文地址:https://www.cnblogs.com/firstForEver/p/7193658.html
Copyright © 2011-2022 走看看