Linux下多线程编程简单示例

       最近尝试下Linux下使用多线程开发程序,在百度文库中找到了一段比较好的范例,介绍的很全面,拿出来和大家分享。

        Linux系统下的多线程遵循POSIX线程接口,称为 pthread。编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a。顺便说一下,Linuxpthread的实现是通过系统调用clone()来实现的。clone()是 Linux所特有的系统调用,它的使用方式类似fork,关于clone()的详细情况,有兴趣的读者可以去查看有关文档说明。下面我们展示一个最简单的多线程程序pthread_create.c

  一个重要的线程创建函数原型:

  1. <span style="font-size:12px;"> #include <pthread.h>   
  2.   
  3.  int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)(void),void *restrict arg);   
  4. </span>  


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

  形式参数:

  pthread_t *restrict tidp要创建的线程的线程id指针

  const pthread_attr_t *restrict attr创建线程时的线程属性

  void* (start_rtn)(void)返回值是void类型的指针函数

  void *restrict arg start_rtn的行参

 

 

例程1: 

功能:向新的线程传递整形值 

 

  1. <span style="font-size:12px;">  
  2. #include <stdio.h>    
  3. #include <pthread.h>    
  4. #include <unistd.h>     
  5.   
  6. void *create(void *arg)     
  7.   
  8. {     
  9.   
  10. int *num;    
  11.   
  12. num = (int *)arg;     
  13. printf("create parameter is %d \n",*num);    
  14. return (void *)0;    
  15.   
  16. }    
  17.   
  18.    
  19.   
  20. int main(int argc ,char *argv[])    
  21.   
  22. {     
  23.   
  24. pthread_t tidp;     
  25.   
  26. int error;     
  27.   
  28. int test=4;    
  29.   
  30. int *attr=&test;    
  31.   
  32. error=pthread_create(&tidp,NULL,create,(void *)attr);     
  33.   
  34. if(error)    
  35.   
  36. {    
  37.   
  38. printf("pthread_create is created is not created ... \n");    
  39.   
  40. return -1;    
  41.   
  42. }     
  43.   
  44. sleep(1);    
  45.   
  46. printf("pthread_create is created ...\n");     
  47.   
  48. return 0;    
  49.    
  50. }     
  51.   
  52. </span>  


编译方法:

 

 gcc -lpthread pthread_int.c -Wall

  

执行结果:

 

 create parameter is 4 

 pthread_create is created is created ... 

 

例程总结:  

可以看出来,我们在main函数中传递的整行指针,传递到我们新建的线程函数中。 

在上面的例子可以看出来我们向新的线程传入了另一个线程的int数据,线程之间还可以传递字符串或是更复杂的数据结构。 

 

例程2: 

  1. <span style="font-size:12px;">程序功能:向新建的线程传递字符串   
  2.   
  3.   
  4.  #include <pthread.h>    
  5.   
  6.  #include <stdio.h>    
  7.   
  8.  #include <unistd.h>    
  9.   
  10.  void *create(void *arg)    
  11.   
  12.  {    
  13.   
  14.  char *name;    
  15.   
  16.  name = (char *)arg;    
  17.   
  18.  printf("The parameter passed from main function is %s \n",name);    
  19.   
  20.  return (void *)0;    
  21.   
  22.  }    
  23.   
  24.  int main(int argc, char *argv[])   
  25.   
  26.  {    
  27.   
  28.  char *a="zieckey";    
  29.   
  30.  int error;    
  31.   
  32.  pthread_t tidp;    
  33.   
  34.  error = pthread_create(&tidp, NULL, create, (void *)a);    
  35.   
  36.  if(error!=0)    
  37.   
  38.  {    
  39.   
  40.  printf("pthread is not created.\n");   
  41.   
  42.  return -1;    
  43.   
  44.  }    
  45.   
  46.  sleep(1);    
  47.   
  48.  printf("pthread is created... \n");    
  49.   
  50.  return 0;    
  51.   
  52.  }  </span>  


 

 编译方法:

 gcc -Wall pthread_string.c -lpthread 

 执行结果: 

 The parameter passed from main function is zieckey  pthread is created... 

 例程总结:

 可以看出来main函数中的字符串传入了新建的线程中。

  

 例程3

  1. <span style="font-size:12px;"> 程序目的:验证新建立的线程可以共享进程中的数据   
  2.   
  3.    
  4.  #include <stdio.h>    
  5.   
  6.  #include <pthread.h>    
  7.   
  8.  #include <unistd.h>    
  9.   
  10.  static int a=4;    
  11.   
  12.  void *create(void *arg)    
  13.   
  14.  {    
  15.   
  16.  printf("new pthread ... \n");    
  17.   
  18.  printf("a=%d \n",a);    
  19.   
  20.  return (void *)0;    
  21.   
  22.  }    
  23.   
  24.  int main(int argc,char *argv[])    
  25.   
  26.  {    
  27.   
  28.  pthread_t tidp;    
  29.   
  30.  int error;    
  31.   
  32.  a=5;    
  33.   
  34.  error = pthread_create(&tidp, NULL, create, NULL);    
  35.   
  36.  if(error!=0)    
  37.   
  38.  {    
  39.   
  40.  printf("new thread is not create ... \n");    
  41.   
  42.  return -1;    
  43.   
  44.  }    
  45.   
  46.  sleep(1);    
  47.   
  48.  printf("new thread is created ... \n");   
  49.   
  50.  return 0;    
  51.   
  52.  }    
  53.   
  54. </span>  


 编译方法: 

 gcc -Wall pthread_share_data.c -lpthread 

 执行结果:

 new pthread ... 

 a=5  

 new thread is created ... 

 例程总结:

 可以看出来,我们在主线程更改了我们的全局变量a的值的时候,我们新建立的线程则打印出来了改变的值,可以看出可以访问线程所在进程中的数据信息。 

 

 2、线程的终止 

 如果进程中任何一个线程中调用exit_Exit,或者是_exit,那么整个进程就会终止,

与此类似,如果信号的默认的动作是终止进程,那么,把该信号发送到线程会终止进程。

 

 线程的正常退出的方式: 

 (1) 线程只是从启动例程中返回,返回值是线程中的退出码 

 (2) 线程可以被另一个进程进行终止 

 (3) 线程自己调用pthread_exit函数 

 两个重要的函数原型: 

  1. <span style="font-size:12px;"> #include <pthread.h>    
  2.  void pthread_exit(void *rval_ptr);  /*rval_ptr 线程退出返回的指针*/    
  3.  int pthread_join(pthread_t thread,void **rval_ptr);  /*成功结束进程为0,否则为错误编码*/    
  4. </span>  

 

 例程 

  1. <span style="font-size:12px;"> 程序目的:线程正常退出,接受线程退出的  
  2.   
  3.   
  4.  #include <stdio.h>    
  5.   
  6.  #include <pthread.h>    
  7.   
  8.  #include <unistd.h>    
  9.   
  10.  void *create(void *arg)    
  11.   
  12.  {    
  13.   
  14.  printf("new thread is created ... \n");    
  15.   
  16.  return (void *)8;   
  17.   
  18.  }    
  19.   
  20.  int main(int argc,char *argv[])   
  21.   
  22.  {    
  23.   
  24.  pthread_t tid;    
  25.   
  26.  int error;    
  27.   
  28.  void *temp;    
  29.   
  30.  error = pthread_create(&tid, NULL, create, NULL);    
  31.   
  32.  if( error )    
  33.   
  34.  {    
  35.   
  36.  printf("thread is not created ... \n");    
  37.   
  38.  return -1;    
  39.   
  40.  }    
  41.   
  42.  error = pthread_join(tid, &temp);    
  43.   
  44.  if( error )    
  45.   
  46.  {    
  47.   
  48.  printf("thread is not exit ... \n");    
  49.   
  50.  return -2;   
  51.   
  52.  }    
  53.   
  54.  printf("thread is exit code %d \n", (int )temp);    
  55.   
  56.  return 0;    
  57.   
  58.  }  </span>  


 

 编译方法:

    gcc -Wall pthread_exit.c -lpthread

 

    执行结果:

 new thread is created ... 

 thread is exit code 8 

 

 例程总结:

  可以看出来,线程退出可以返回线程的int数值。线程退出不仅仅可以返回线程的int数值,还可以返回一个复杂的数据结构。

 

 例程 

[html]  view plain copy print ?
  1. <span style="font-size:12px;"> #include <stdio.h>    
  2.   
  3.  #include <pthread.h>    
  4.   
  5.  #include <unistd.h>    
  6.   
  7.  struct menber    
  8.   
  9.  {    
  10.   
  11.  int a;    
  12.   
  13.  char *b;    
  14.   
  15.  }   
  16.   
  17. temp = {8,"zieckey"};    
  18.   
  19. void *create(void *arg)    
  20.   
  21. {    
  22.  printf("new thread ... \n");    
  23.  return (void *)&temp;    
  24. }    
  25.   
  26. int main(int argc,char *argv[])    
  27.   
  28. {    
  29.   
  30.  int error;    
  31.   
  32.  pthread_t tid;   
  33.   
  34.  struct menber *c;    
  35.   
  36.  error = pthread_create(&tid, NULL, create, NULL);    
  37.   
  38.  if( error )    
  39.   
  40.  {    
  41.   
  42.  printf("new thread is not created ... \n");    
  43.   
  44.  return -1;    
  45.   
  46.  }    
  47.   
  48.  printf("main ... \n");    
  49.   
  50.  error = pthread_join(tid,(void *)&c);    
  51.   
  52.  if( error )    
  53.   
  54.  {    
  55.   
  56.  printf("new thread is not exit ... \n");    
  57.   
  58.  return -2;    
  59.   
  60.  }   
  61.   
  62.  printf("c->a = %d \n",c->a);    
  63.   
  64.  printf("c->b = %s \n",c->b);    
  65.   
  66.  sleep(1);    
  67.   
  68.  return 0;    
  69.   
  70.  }  </span>  


 

 编译方法:

 gcc -Wall pthread_return_struct.c -lpthread 

 执行结果: 

 main ...  

 new thread ...

 c->a = 8  

 c->b = zieckey 

 例程总结:

   一定要记得返回的数据结构要是在这个数据要返回的结构没有释放的时候应用, 如果数据结构已经发生变化,那返回的就不会是我们所需要的,而是脏数据 

 

 3、线程标识 

 函数原型:

  1. <span style="font-size:12px;"> #include <pthread.h>    
  2.   
  3.  pthread_t pthread_self(void);    
  4.   
  5.  pid_t getpid(void);    
  6. </span>  

 

 getpid()用来取得目前进程的进程识别码,函数说明 

 

 例程 

  1. <span style="font-size:12px;">   
  2.  #include <stdio.h>    
  3.   
  4.  #include <pthread.h>    
  5.   
  6.  #include <unistd.h> /*getpid()*/    
  7.   
  8.  void *create(void *arg)    
  9.   
  10.  {    
  11.   
  12.  printf("New thread .... \n");   
  13.   
  14.  printf("This thread's id is %u \n", (unsigned int)pthread_self());    
  15.   
  16.  printf("The process pid is %d \n",getpid());   
  17.   
  18.  return (void *)0;    
  19.   
  20.  }    
  21.   
  22.  int main(int argc,char *argv[])    
  23.   
  24.  {    
  25.   
  26.  pthread_t tid;    
  27.   
  28.  int error;    
  29.   
  30.  printf("Main thread is starting ... \n");    
  31.   
  32.  error = pthread_create(&tid, NULL, create, NULL);    
  33.   
  34.  if(error)    
  35.   
  36.  {    
  37.   
  38.  printf("thread is not created ... \n");    
  39.   
  40.  return -1;    
  41.   
  42.  }    
  43.   
  44.  printf("The main process's pid is %d \n",getpid());    
  45.   
  46.  sleep(1);    
  47.   
  48.  return 0;    
  49.   
  50.  }  </span>  


 

 编译方法: 

 gcc -Wall -lpthread pthread_id.c 

 执行结果:

 Main thread is starting ... 

 The main process's pid is 3307 

 New thread .... 

 This thread's id is 3086347152 

 The process pid is 3307

猜你喜欢

转载自huaonline.iteye.com/blog/1753132