zoukankan      html  css  js  c++  java
  • linux多线程入门

        linux下的多线程通过pthread实现,下面给个简单的例子。

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    void* thr_fn()
    {
        printf("this is a thread, tid = %d
    ", pthread_self());
        printf("thread return
    ");
        return (void*)0;
    }
    
    int main()
    {
        printf("this is the main thread, pid = %d
    ", getpid());
        pthread_t tid;
        int ret;
        ret = pthread_create(&tid, NULL, thr_fn, NULL);
        if (ret != 0)
        {
            printf("create thread error
    ");
            exit(1);
        }
        pthread_join(tid, NULL);
        printf("main thread return
    ");
        return 0;
    }

    执行输出如下:

     

        主要涉及两个函数:

     int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                              void *(*start_routine) (void *), void *arg);      //线程创建函数
      int pthread_join(pthread_t thread, void **retval);                        //等待线程结束函数

     reference:

    Linux下的多线程编程

  • 相关阅读:
    P、NP、NPC、NPH问题介绍
    过河卒 bfs搜索
    对迪杰斯特拉算法的理解
    第七周
    周作业
    月考一
    第四周
    第三周
    第二周作业
    46期第一次作业
  • 原文地址:https://www.cnblogs.com/gattaca/p/4729244.html
Copyright © 2011-2022 走看看