天天看点

Pthreads线程的基本常识

<b>1.Pthreads</b><b>中线程的接口函数</b>

<b></b>

与线程相关的头文件为pthread.h,编译时要连接的库为pthread。

pthread_t thread;

线程的数据类型为pthread_t。

int pthread_equal(pthread t1,pthread t2);

判断两个线程标识符是不是代表的是同一个线程。线程标识符只有相等和不等的关系,没有大小关系。

int pthread_create(pthread_t * thread,

               const pthread_attr_t * attr, void *(*start)(void *),void * arg);

pthread_create函数将创建一个线程,第一个传出参数为系统分配给线程的标识符。第二个参数为线程设置的属性。第三个参数为线程开始运行后执行的启动函数。第四个参数为函数要用到的参数。

pthread_t pthread_self(void);

pthread_self函数将获得当前线程的标识符。返回值即为当前线程的标识符。获得线程标识符的方法有两种:(1)使用pthread_create函数.(2)使用pthread_self函数.

int sched_yield(void);

志愿让出处理器的使用权给其他线程使用。

int pthread_exit(void * value_ptr);

当前线程退出。

int pthread_detach(pthread_t thread);

当线程进入终止状态时,清理线程所占资源后进入回收状态。

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

阻塞等待thread线程执行结束,并得到该线程的返回值。注意第二个参数应该为指针的指针。该函数内部会负责调用pthread_detach保证线程的资源得到释放。

<b> </b>

<b>2.</b><b>主线程和普通线程的区别</b>

(1)主线程使用return、exit、或者正常执行到主函数的结尾退出时,所有的线程都将退出。如果主线程需要其他线程执行结束后才退出,可以使用以下方式:1)调用pthread_join,这时调用的主线程将被阻塞,直到join的线程执行结束后,才接着往下执行。2)调用pthread_exit函数,但是pthread_exit函数返回主线程后,系统不能正常得到程序的返回状态。3)使用条件变量等待其他线程执行结束。

(2)主线程的参数传递方式和普通线程不一样,主线程通过main函数中的argc和argv得到各个参数,而普通线程需要将所有的参数封装到一起,传给线程的参数。

(3)一般主线程的栈的大小比普通现成的大很多。主线程使用的是进程的栈,所以会比较大。

(4)主线程的main函数是被外部的某个函数调用的,一般是链接的crt0.o这个文件。ctr0(C running time)是一个启动例程,负责完成进程的初始化工作后,调用main函数。普通线程直接调用start函数。

<b>3.</b><b>线程的资源回收</b>

         线程执行完毕之后应该是放其所占用的资源:包括虚拟内存,堆栈,锁,信号量等信息。一般线程调用了pthread_cancel,pthread_exit或者执行完其start函数中的逻辑或者执行完了return语句,线程的状态将进入终止状态。进入终止状态的线程并没有释放它所占用的资源,一般有以下三种方式:

         (1)创建线程的时候在第二个参数中设置detach属性。

         (2)使用pthread_detach函数

         (3)使用pthread_join函数

         线程所占用的资源有些被释放,有些被回收利用。在启动新的线程的时候可以减少一部分开销。

         线程的分离(detach)同线程执行其逻辑没有必然联系。还有需要注意的是,一个线程只能被join一次,如果有多个线程都需要等待一个线程,使用信号量实现。

         pthread_cancel结束的线程,其返回值只有一种:PTHREAD_CANCELLED

<b>4.</b><b>线程的返回值</b>

<b>         </b>使用线程的返回值最容易出现的错误就是使用了线程内部的栈中数据的地址作为返回值。A线程创建了B线程,B线程可能会在A线程开始执行join之前,已经detach掉了。其栈早已失效,这时如果线程内部返回的是栈中某数据的地址一定出错。

         一般线程的返回值可以使用以下三种方式:

         (1)使用全局变量

         (2)线程内部使用malloc开辟空间,回传给需要的线程。该线程负责释放这段内存

         (3)使用传出参数,即调用线程使用malloc在堆上开辟空间,并负责数据的维护

<b>5.</b><b>代码示例</b>

#include&lt;stdio.h&gt; 

#include&lt;pthread.h&gt; 

#include "../error.h" 

void * thread_routin(void * arg) 

        return arg; 

int main(int argc,char * argv[]) 

        pthread_t thread; 

        void * thread_result; 

        int status; 

        status = pthread_create(&amp;thread,NULL,thread_routin,NULL); 

        if(0 != status) 

                err_abort("create thread error.",status); 

        status = pthread_join(thread,&amp;thread_result); 

                err_abort("join thread error.",status); 

        if(NULL != thread_result) 

                fprintf(stderr,"%s","Return value error.\n"); 

        return 0; 

以上程序是一个简单的参数传入传出的示例程序。 

#include&lt;stdlib.h&gt; 

        status = pthread_join(thread,NULL); 

        { 

                fprintf(stderr,"%s \"%s\":%d: %s\n","thread join error",__FILE__,__LINE__,strerror(status)); 

                abort(); 

        } 

以上程序是一个故意出错的程序,在Linux中程序会直接崩溃,提示“段错误”。

#include&lt;time.h&gt; 

void * thread_routin(void *arg) 

        sleep(5); 

int main(int argc,char *argv[]) 

        pthread_t thread ; 

        int result; 

                err_abort("creating thread error.",status); 

        result = 1; 

        pthread_exit((void *)&amp;result); 

        //return 0; 

这是一个测试main线程调用pthread_exit能否返回需要的值的测试程序。该程序表明:在main线程中使用pthread_exit虽然能保证其他线程执行完毕,但是在系统无法得知整个程序的最后执行状态。

        //pthread_exit(arg); 

        void * result; 

        int set_result = -1; 

        status = pthread_create(&amp;thread,NULL,thread_routin,(void *)&amp;set_result); 

        status = pthread_join(thread,&amp;result); 

                err_abort("joining thread error.",status); 

        printf("exit value is %d\n",*((int *)result)); 

        pthread_exit((void*)&amp;set_result); 

该程序对普通线程和主线程分别调用pthread_exit以测试线程的返回值。主线程的返回值系统无法得到,普通线程的返回值却可以得到。

本文转自hipercomer 51CTO博客,原文链接:http://blog.51cto.com/hipercomer/909941

继续阅读