分享

Linux 线程学习(一)

 astrotycoon 2013-09-05

     学习资料来自APUE 这本著名的书籍,部分来自wikipedia,我将分步骤一步一步的将自己的学到的东西分享出来!

  首先给出的整体的内容

     

1)基本概念

   线程,又称为轻量级线程,是程序执行流的最小单元!进程是分为用户级线程和内核级线程!

 常见的模型有

    线程:进程

     1 :1   指的是 kernel-level thread  ,on Linux  usual c library implement this approach!

     N: 1  指的是 用户级线程! An N:1 model implies that all application-level threads map to a single kernel-level scheduled entity; the kernel has no knowledge of the application threads. With this approach, context switching can be done very quickly and, in addition, it can be implemented even on simple kernels which do not support threading

     对于用户级线程,其好处是能够快速的进行context switch 转换,因为它们之间是没有进入到内核模式下的!缺点就是,不能够利用硬件来加速!因为每次只能有一个thread

能够被processor调用!

2)特点

  线程,是对进程进一步的细分,它的引入是为了更好并发执行程序,提高执行效率。线程之间共享进程资源,一个线程是可以创建或者是撤销一个线程。

  在线程 中 context switch 速度 比在进程间的速度要快很多。线程包含必需的系统资源,比如pc,一些必须的寄存器和堆栈,线程id等!

 3)与进程的区别

    1)进程含有独立的地址空间,而线程是没有的,线程必须在进程中才有!线程是必须依靠线程才能够生存 。一个进程可以看作是一个独立的线程

    2)线程中含有少量的自己必需的状态信息,而进程中则包含大量的信息!

    3)在进程中进行context switch 比在线程中context switch 慢很多!

 4) 线程的创建以及终止

       首先介绍几个比较重要的函数

       这些函数的头文件都是#include<pthread.h>

       int pthread_equal(pthread_t tid1,pthread_t tid2)//比较两个线程id是否相等

       pthread_t  pthread_self(void)//返回的是自己的线程id

       int pthread(pthread_t *restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)(void),void *restrict arg);//创建一个id

       给出一段代码做参考!

       

复制代码
 1 #include<pthread.h>
 2 #include<stdio.h>
 3 void * func(void *arg)
 4 {
 5   printf("the pid of the programming is %d\n",getpid());
 6   printf("the new thread is :%u\n",pthread_self());
 7   return ((void*) 0);
 8 }
 9 int main()
10 {
11    pthread_t tid=pthread_self();//tid 是main这个线程的id
12    pthread_t ntid; 
13    printf("the main thread is %u:\n",tid);
14    pid_t pid=getpid();
15    printf("the pid of the process is %d:\n",pid);
16    int err=pthread_create(&ntid,NULL,func,NULL);
17    if(err!=0)
18      printf("create thread error\n");
19    sleep(1);
20    return 0;
21 
22 }
复制代码

  从这个程序中我么可以看到新建的线程和以前的线程是在同一个进程中的,但是它们的线程id是不同的!

 线程的终止:

  1)不能够使用一些比较常用的函数 ,比如说_exit,exit 等函数,在任何一个线程中使用这些函数,将使得进程给终止。

  2)有三种方式来终止一个线程

       1.线程被其他的线程给取消

       2.线程调用pthread_exit() 函数

       3.线程从启动例程中返回,返回的是线程的退出码!

   我们通常用pthread_join()函数来取出线程的退出码

  给出个简单利用的例子

  

复制代码
 1 #include<pthread.h>
 2 #include<stdio.h>
 3 void * func(void *arg)
 4 {
 5         printf("the thread1 exit \n");
 6         pthread_exit((void *)1);
 7 }
 8 void * func1(void *arg)
 9 {
10         printf("the thread2 exit \n");
11         pthread_exit((void *)2);
12 }
13 int main()
14 {
15         int err;
16         pthread_t tid1,tid2;
17         void *tret;
18         err=pthread_create(&tid1,NULL,func,NULL);
19         if(err!=0)
20                 printf("can't create thread 1:\n");
21         err=pthread_create(&tid2,NULL,func1,NULL);
22         if(err!=0)
23                 printf("can't create thread 2:\n");
24         err=pthread_join(tid1,&tret);
25         if(err!=0)
26                 printf("can't join thread1\n");
27         printf("thread1 exit code %d\n",(int)tret);
28         err=pthread_join(tid2,&tret);
29         if(err!=0)
30                 printf("can't join thread2\n");
31         printf("thread2 exit code %d\n",(int)tret);
32         return 0;
33 
34 
35 
36 
37 }
复制代码

注意编译的时候 应该是 gcc -o  ... .... -lpthread

5)线程的同步将在下一篇讲解!

  

  

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多