线程

来源:互联网 发布:七天网络成绩查询,网页 编辑:程序博客网 时间:2024/05/20 13:15
  1. /***getpid()函数************************************************
  2. *    函数功能:    取得进程ID
  3. *    函数原型: 
  4. *    返回值 :    
  5. *                
  6. *    所需库 :    
  7. * 备注 :    
  8. ****************************************************************/

  9. /***pthread_self()函数*******************************************
  10. *    函数功能:    获取当前线程的ID
  11. *    函数原型: pthread_t pthread_self(void)
  12. *    返回值 :    
  13. *                
  14. *    所需库 : <pthread.h>    
  15. * 备注 : pthread_t的类型为unsighned long int,打印时用 %u/%lu,
  16. *             否则显示结果出问题     
  17. *****************************************************************/
  1. /******************************************
  2. 进程的主线程
  3.     当一个程序启动时,就有一个进程被操作系统(OS)创建,与此同时一个线程也立刻运行,
  4. 该线程通常叫做程序的主线程(Main Thread),因为它是程序开始时就执行的,如果你需要
  5. 再创建线程,那么创建的线程就是这个主线程的子线程。每个进程至少都有一个主线程,
  6. 在Winform中,应该就是创建GUI的线程。 
  7. 主线程的重要性体现在两方面:
  8.     1.是产生其他子线程的线程;
  9.     2.通常它必须最后完成执行比如执行各种关闭动作。
  10. *****************************************/
  11. int main(){
  12.     int err;

  13.     err = pthread_create(&ntid,NULL,thr_fn,NULL);//---------------------------->func3创建线程
  14.     if(err != 0){//--------------------------->这里说明返回0表示创建线程成功!
  15.     printf("can't create thread: %s\n",strerror(err));
  16.     return 1;
  17.     }

  18.     print_id_mes("this is main thread:");//后被主线程调用 
  19.     sleep(1);
  20.     return 0;
  21. }


printf("%s start pid %d thread_id=%d\n",__FUNCTION__,getpid(),(int)pthread_self());//
pthread_t pthread_self(void);
函数作用:获得线程自身的ID。pthread_t的类型为unsigned long int,所以在打印的时候要使用%lu方式,否则将产生神奇的结果
线程传参数:
pthread_create(&g_stRtpConvert.stMain[iChn].thConvert,NULL,thread_rtp_convert,(void *)&g_stRtpConvert.stMain[iChn]);
void * thread_rtp_convert(void *rtp)
{
printf("%s start pid %d thread_id=%d\n",__FUNCTION__,getpid(),(int)pthread_self());
ST_RTP_CONVERT_THREAD * stp_rtp_convert=(ST_RTP_CONVERT_THREAD *)rtp;


0 0