zoukankan      html  css  js  c++  java
  • Linux 进程与线程五

    pthread_self函数
    pthread_t pthread_self(void);
    一般会成功,返回当前线程的ID
    注意:在子线程中执行exit()函数会退出整个进程,一般使用pthread_exit函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void *thread_run(void *arg)
    {
        int i=0;
        for(i=0;i<10;i++)
        {
            if(i==5)
            {
                printf("线程1退出了!
    ");
                //注意:在子线程中执行exit()函数会退出整个进程,一般使用pthread_exit函数
                exit(0);
            }
            printf("%d
    ",i);
            sleep(1);
        }
        return NULL;
    }
    
    int main(int arg,char *args[])
    {
        pthread_t thr1;
        if(pthread_create(&thr1,NULL,thread_run,NULL)!=0)
        {
            printf("pthread_create() failed !
    ");
            return -1;
        }
        //等待线程1
        pthread_join(thr1,NULL);
        printf("主线程完结1!
    ");
        return 0;
    }
  • 相关阅读:
    hdu-1114
    hdu2546
    POJ-3126
    POJ-1915
    ZOJ-1709
    Codeforces 847H
    Codeforces 847C
    Codeforces 847I
    Codeforces 847E
    算法笔记--矩阵及矩阵快速幂
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/6233673.html
Copyright © 2011-2022 走看看