日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 线程学习初步01

發布時間:2023/11/30 linux 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 线程学习初步01 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 線程的概念

    進程與線程內核實現 通過函數clone實現的
  • ps -Lf pid
  • Linux內核線程實現原理

    同一個進程下的線程,共享該進程的內存區, 但是只有stack區域不共享。

  • 線程共享資源
    a.文件描述符表
    b.每種信號的處理方式
    c.當前工作目錄
    d.用戶id和組id

  • 線程非共享資源
    a.線程id
    b.處理器現場和棧指針(內核棧)
    c.獨立的棧空間(用戶空間棧)
    d.errno變量
    e.信號屏蔽字
    f.調度優先級

  • 在主線程里面執行return, 相當于整個進程退出了

  • 小技巧
    set -o vi 相當于把當前shell,弄成了 vi 編輯器模式

  • 7.創建一個線程
    man pthread_create

    #include <pthread.h>int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);Compile and link with -pthread.

    #include <pthread.h> #include <unistd.h> #include <stdio.h>void* func(void *arg) {printf("I am a common thread, pid is %d, tid is %ld\n", getpid(), pthread_self());pthread_exit(NULL); }int main() {pthread_t tid;pthread_create(&tid, NULL, func, NULL);printf("I am a man thread, pid is %d,create tid is %ld\n", getpid(), tid);printf("I am a man thread, pid is %d, tid is %ld\n", getpid(), pthread_self());pthread_exit(NULL);return 0; }經測試,主線程使用pthread_exit函數,可以等待子線程的退出。

    線程退出函數:

    8.線程回收函數:

    int pthread_join(pthread_t thread, void **retval); 參數介紹:thread: 表示要回收的線程(創建線程時候傳出的第一個參數)retval:要回收的線程的退出信息

    線程回收也是阻塞等待回收
    代碼案例:

    #include <pthread.h> #include <unistd.h> #include <stdio.h>void* func(void *arg) {printf("I am a common thread, pid is %d, tid is %ld\n", getpid(), pthread_self());// pthread_exit((void *)100);return (void*)(100); }int main() {pthread_t tid;pthread_create(&tid, NULL, func, NULL);printf("I am a man thread, pid is %d,create tid is %ld\n", getpid(), tid);printf("I am a man thread, pid is %d, tid is %ld\n", getpid(), pthread_self());void * ret;pthread_join((tid), &ret);printf("join tid return value is %d\n", (int)ret);return 0; }

    總結

    以上是生活随笔為你收集整理的linux 线程学习初步01的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。