天天看點

進線程——線程建立退出

程序:每一個程序互相獨立,即使一個程序結束也沒有太大影響。但在任務切換時需要頻繁重新整理cache緩存。故後面引出輕量級程序---線程

線程也是最小的任務排程機關

一個程序建立的多個線程可以共享一個程序的位址空間及相關資源

線程共享資料:

  1.         使用者名、使用者組名

  2.         靜态資料和全局變量

  3.         檔案描述符

非共享資料:

  1.         線程id

  2.         狀态、優先級

  3.         PC

  4.         堆棧

線程相關接口函數

        pthread_creat()   --建立線程

        pthread_exit()     --退出線程

        pthread_join()     --等待線程結束

(1)線程建立

#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                    void *(*start_routine) (void *), void *arg);
           

參數:

        thread:線程對象(線程的編号)

        attr:線程屬性,填NULL表示使用預設屬性

        start_routine:調用函數首位址

        arg:函數形參參數,如果沒有參數就填NULL

傳回值:成功傳回0,建立失敗傳回錯誤碼(小于0)

注意:

        如果主線程先結束,整個程式都會結束。

示例代碼:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a = 5;
typedef struct
{
    int a;
    int b;
}m_arg;


void *funct(void *head)     //建立的線程
{
    m_arg  *b = (m_arg *)head;
    printf("%d\n",b->b);
    while((b->a)--)
    {
        printf("aaaaaaaa%d\n",b->a);
        sleep(1);
    }
}

int main(int argc, char *argv[])
{ 
    m_arg arr = {5,6};
    pthread_t thread;
    printf("%d\n",a);
    int ret = pthread_create(&thread,NULL,funct,&arr);//建立線程
    if(ret != 0)                          //判斷建立是否成功
    {
        perror("pthread_create");
        exit(0);
    }
    while(1)   //主線程
    {
        printf("bbbbbbbb\n");
        sleep(1);
    }
    return 0;
} 
           

(2)退出線程

 #include <pthread.h>

  void pthread_exit(void *retval)
           

參數:

        retval:線程結束傳回的資訊,可以是任意資訊,一般是字元串,沒有可以填NULL

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a = 5;
typedef struct
{
    int a;
    int b;
}m_arg;


void *funct(void *head)     //建立的線程
{
    m_arg  *b = (m_arg *)head;
    printf("%d\n",b->b);
    while((b->a)--)
    {
        if(b->a == 3)
            pthread_exit(NULL);      //等待秒關閉線程
        printf("aaaaaaaa%d\n",b->a);
        sleep(1);
    }
}

int main(int argc, char *argv[])
{ 
    m_arg arr = {5,6};
    pthread_t thread;
    printf("%d\n",a);
    int ret = pthread_create(&thread,NULL,funct,&arr);//建立線程
    if(ret != 0)                          //判斷建立是否成功
    {
        perror("pthread_create");
        exit(0);
    }
    while(1)   //主線程
    {
        printf("bbbbbbbb\n");
        sleep(1);
    }
    return 0;
} 
           

(3)等待線程退出

#include <pthread.h>

int pthread_join(pthread_t thread,void **retval) ;
           

參數:

        thread:線程對象(線程編号)

        retval:接受傳回的字元串資訊

傳回值:

        成功傳回0,建立失敗傳回錯誤碼。

#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

typedef struct
{
    int a;
    int b;
}m_arg;


void *funct(void *head)          //線程2函數
{
    m_arg  *b = (m_arg *)head;
    printf("%d\n",b->b);
    while((b->a)--)
    {
        if(b->a == 3)
            pthread_exit("thread_funtc1");
        printf("aaaaaaaa%d\n",b->a);
        sleep(1);
    }
}

void *funtc2()            //線程1函數
{
    int n = 5;
    while(n--)
    {
        if(n == 2)
            pthread_exit("thread_funtc2");
        printf("cccccccc\n");
        sleep(1);
    }
}
int main(int argc, char *argv[])
{ 
    m_arg arr = {5,6};
    pthread_t thread;
    pthread_t thread2;
    int ret = pthread_create(&thread,NULL,funct,&arr);    //建立線程1
    if(ret != 0)
    {
        perror("pthread_create");
        exit(0);
    }
    int ret1 = pthread_create(&thread2,NULL,funtc2,NULL); //建立線程2
    if(ret1 != 0)
    {
        perror("pthread_create2");
        exit(0);
    }
    void *result = NULL;
    pthread_join(thread,&result);    //等待線程1退出
    printf("%s\n",(char *)result);
    pthread_join(thread,&result);    //等待線程2退出
    printf("%s\n",(char *)result);
    printf("bbbbbbbb\n");
    sleep(1);
    return 0;
} 
           

繼續閱讀