zoukankan      html  css  js  c++  java
  • linux中C/C++多线程

     ·线程创建

      函数原型:int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);

      返回值:若是成功建立线程返回0,否则返回错误的编号。

      形式参数:pthread_t *restrict tidp要创建的线程的线程id指针;const pthread_attr_t *restrict attr创建线程时的线程属性;void* (start_rtn)(void)返回值是void类型的指针函数;void *restrict arg start_rtn的形参。

      ·线程挂起:该函数的作用使得当前线程挂起,等待另一个线程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。

      函数原型:int pthread_join( pthread_t thread, void **value_ptr);

      参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。

      ·线程退出

      函数原型:void pthread_exit(void *rval_ptr);

      ·获取当前线程id

      函数原型:pthread_t pthread_self(void);

      ·互斥锁

      创建pthread_mutex_init;销毁pthread_mutex_destroy;加锁pthread_mutex_lock;解锁pthread_mutex_unlock。

      ·条件锁

      创建pthread_cond_init;销毁pthread_cond_destroy;触发pthread_cond_signal;广播pthread_cond_broadcast S;等待pthread_cond_wait。 

      ·正确处理Linux平台下的线程结束问题

      在Linux平台下,当处理线程结束时需要注意的一个问题就是如何让一个线程善始善终,让其所占资源得到正确释放。在Linux平台默认情况下,虽然各个线程之间是相互独立的,一个线程的终止不会去通知或影响其他的线程。但是已经终止的线程的资源并不会随着线程的终止而得到释放,我们需要调用pthread_join() 来获得另一个线程的终止状态并且释放该线程所占的资源。C

    前言:

    1.linux线程执行和windows不同,pthread有两种状态joinable状态和unjoinable状态,如果线程是joinable状态,当线程函数自己返回退出时或pthread_exit时都不会释放线程所占用堆栈和线程描述符(总计8K多)。只有当你调用了pthread_join之后这些资源才会被释放。若是unjoinable状态的线程,这些资源在线程函数退出时或pthread_exit时自动会被释放。

    2.unjoinable属性可以在pthread_create时指定,或在线程创建后在线程中pthread_detach自己, 如:pthread_detach(pthread_self()),将状态改为unjoinable状态,确保资源的释放。或者将线程置为 joinable,然后适时调用pthread_join.

    3.其实简单的说就是在线程函数头加上 pthread_detach(pthread_self())的话,线程状态改变,在函数尾部直接 pthread_exit线程就会自动退出。省去了给线程擦屁股的麻烦。

    eg:

         pthread_t tid;
         int status = pthread_create(&tid, NULL, ThreadFunc, NULL);
         if(status != 0)
         {
          perror("pthread_create error");
         }
         pthread_detach(tid);
    

    一:pthread_join()

    (1)pthread_join()即是子线程合入主线程,主线程阻塞等待子线程结束,然后回收子线程资源。

    (2)函数说明

    1)头文件 : #include <pthread.h>

    2)函数定义: int pthread_join(pthread_t thread, void **retval);

    3)描述 :pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。

    4)参数 :thread: 线程标识符,即线程ID,标识唯一线程。retval: 用户定义的指针,用来存储被等待线程的返回值。

    5)返回值 : 0代表成功。 失败,返回的则是错误号。

    (3)实例

        #include <pthread.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <stdio.h>
         
        void *thread_function(void *arg)
        {
        int i;
        for ( i=0; i<8; i++)
        {
        printf("Thread working...! %d 
    ",i);
        sleep(1);
        }
        return NULL;
        }
         
        int main(void)
        {
        pthread_t mythread;
         
        if ( pthread_create( &mythread, NULL, thread_function, NULL) )
        {
        printf("error creating thread.");
        abort();
        }
        if ( pthread_join ( mythread, NULL ) )
        {
        printf("error join thread.");
        abort();
        }
         
        printf("thread done! 
    ");
        exit(0);
        }
    

    结果:

    去掉pthread_join ( mythread, NULL )

        #include <stdlib.h>
        #include <unistd.h>
        #include <stdio.h>
         
        void *thread_function(void *arg)
        {
        int i;
        for ( i=0; i<8; i++)
        {
        printf("Thread working...! %d 
    ",i);
        sleep(1);
        }
        return NULL;
        }
         
        int main(void)
        {
        pthread_t mythread;
         
        if ( pthread_create( &mythread, NULL, thread_function, NULL) )
        {
        printf("error creating thread.");
        abort();
        }
        /*
        if ( pthread_join ( mythread, NULL ) )
        {
        printf("error join thread.");
        abort();
        }
        */
         
        printf("thread done! 
    ");
        exit(0);
        }
    

    结果:

    二:pthread_detach()

    (1)pthread_detach()即主线程与子线程分离,子线程结束后,资源自动回收。

    (2)函数说明

    1)函数原型:int pthread_detach(pthread_t tid);

    2)功能:pthread_join()函数的替代函数,可回收创建时detachstate属性设置为PTHREAD_CREATE_JOINABLE的线程的存储空间。该函数不会阻塞父线程。pthread_join()函数用于只是应用程序在线程tid终止时回收其存储空间。如果tid尚未终止,pthread_detach()不会终止该线程。当然pthread_detach(pthread_self())也是可以得

    3)头文件:#include <pthread.h>     pthread非linux系统的默认库, 需手动链接-线程库 -lpthread

    4)参数:tid:线程标识符

    5)返回值:pthread_detach() 在调用成功完成之后返回零。其他任何返回值都表示出现了错误。如果检测到以下任一情况,pthread_detach()将失败并返回相应的值。

    EINVAL:tid是分离线程

    ESRCH:tid不是当前进程中有效的为分离线程

    (3)实例

        #include <stdlib.h>
        #include <unistd.h>
        #include <stdio.h>
        void print_message_function( void *ptr );
        main ( )
        {
        pthread_t thread1;
        pthread_create(&thread1,NULL,(void *)&print_message_function,(void *)0);
        int i;
        for(i=0;i<5;i++)
        {
        printf("%d
    ",thread1);
        }
         
        exit (0) ;
        }
        void print_message_function( void *ptr )
        { pthread_detach(pthread_self());
        static int g;
        printf("%d
    ", g++);
         
        pthread_exit(0) ;
        }
    

    结果:

    //pthread_detach(pthread_self());
    //使线成分离出来。当这个线程执行完成任务后释放释放资源。不然它会保留退出状态,等待别人来取。
    pthread_detach(threadid)和pthread_detach(pthread_self())没有什么区别吧!有很严格的区别吗???如果非要讲区别不可,我觉得应该是调用他们的线程不同。
          pthread_detach(threadid)函数的功能是使线程ID为threadid的线程处于分离状态,一旦线程处于分离状态,该线程终止时底 层资源立即被回收;否则终止子线程的状态会一直保存(占用系统资源)直到主线程调用pthread_join(threadid,NULL)获取线程的退 出状态。通常是主线程使用pthread_create()创建子线程以后,一般可以调用pthread_detach(threadid)分离刚刚创建的子线程,这里的threadid是指子线程的threadid;如此以来,该子线程止时底层资源立即被回收;被创建的子线程也可以自己分离自己,子线程调用pthread_detach(pthread_self())就是分离自己,因为pthread_self()这个函数返回的就是自己本身的线程ID;

  • 相关阅读:
    iOS开发学习之NSFetchedResultsController
    iOS开发学习之Core Data
    iOS开发学习之MapKit
    iOS学习之CoreLocation相关知识
    制作LaunchImage的简单方法
    iOS学习之table view controller、table view cell
    iOS学习之NSKeyedArchiver、NSKeyedUnarchiver归档
    【MM 】委外加工退货
    【SAP Workflow】相关英语专业术语
    【MM】Purchasing Account Management---待完善
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/14346845.html
Copyright © 2011-2022 走看看