Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1975242
  • 博文数量: 610
  • 博客积分: 11499
  • 博客等级: 上将
  • 技术积分: 5511
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-12 19:27
文章分类

全部博文(610)

文章存档

2016年(5)

2015年(18)

2014年(12)

2013年(16)

2012年(297)

2011年(45)

2010年(37)

2009年(79)

2008年(101)

分类:

2012-04-30 10:34:46

原文地址:C语言多线程 作者:aoyang888

基本概念  进程:当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和。而一个进程又是由多个线程所组成的。
  线程:线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈指针、程序计数器等),但代码区是共享的,即不同的线程可以执行同样的函数。
  多线程:多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。
多线程优势  线程程序作为一种多任务、并发的工作方式,当然有其存在优势:
  ·提高响应:这对图形界面的程序尤其有意义,当一个操作耗时很长时,整个系统都会等待这个操作,此时程序不会响应键盘、鼠标、菜单的操作,而使用多线程技术,将耗时长的操作(time consuming)置于一个新的线程,可以避免这种尴尬的情况。
  ·使多CPU系统更加有效:会保证当线程数不大于CPU数目时,不同的线程运行于不同的CPU上。
  · 改善程序结构:一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。
Linux C多线程介绍  C语言的开始设计,并未设计多线程的机制,由于随着软硬件的发展及需求的发展。后来C语言才发开了线程库以支持多线程的操作、应用。
  主要基于Linux介绍C多线程。在编译C的多线程时候,一方面必须指定Linux C语言线程库多线程库pthread,才可以正确编译(例如:gcc test.c -o test -lpthread);另一方面要包含有关线程头文件#include
C多线程操作  ·线程创建
  函数原型:int ( *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_t thread, void **value_ptr);
  参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。
  ·线程退出
  
函数原型:void (void *rval_ptr);
  ·获取当前线程id
  函数原型:pthread_t (void);
  ·互斥锁
  创建pthread_mutex_init;销毁pthread_mutex_destroy;加锁pthread_mutex_lock;解锁pthread_mutex_unlock。
  ·条件锁
  创建;销毁pthread_cond_destroy;触发pthread_cond_signal;广播pthread_cond_broadcast S;等待pthread_cond_wait。 
  ·正确处理Linux平台下的线程结束问题
  在Linux平台下,当处理线程结束时需要注意的一个问题就是如何让一个线程善始善终,让其所占资源得到正确释放。在Linux平台默认情况下,虽然各个线程之间是相互独立的,一个线程的终止不会去通知或影响其他的线程。但是已经终止的线程的资源并不会随着线程的终止而得到释放,我们需要调用pthread_join() 来获得另一个线程的终止状态并且释放该线程所占的资源。
  ·例程:下面通过一个简单例程example.c来展示Linux下的C语言多线程操作。
  /* example.c*/
  #include
  #include
  void thread(void)
  {
  int i;
  for(i=0;i<3;i++)
  printf("This is a pthread.\n");
  }
  int main(void)
  {
  pthread_t id;
  int i,ret;
  ret=pthread_create(&id,NULL,(void *) thread,NULL);
  if(ret!=0){
  printf ("Create pthread error!\n");
  exit (1);
  }
  for(i=0;i<3;i++)
  printf("This is the main process.\n");
  pthread_join(id,NULL);
  return (0);
  }
  编译此程序:gcc example.c -lpthread -o example。
  运行example得到如下结果(结果有多样性,根据具体系统的调用算法不同而不同):
  This is the main process.
  This is a pthread.
  This is the main process.
  This is the main process.
  This is a pthread.
  This is a pthread.
  =====================================================================
  linux 下c多线程实例
  #include
  #include
  #include
  #include <>
  #include
  #define MAX 10
  pthread_t thread[2];
  pthread_mutex_t mut;
  int number=0, i;
  void * thread1( void *)
  {
  printf ("thread1 : I'm thread 1\n");
  for (i = 0; i < MAX; i++)
  {
  printf("thread1 : number = %d\n",number);
  pthread_mutex_lock(&mut);
  number++;
  pthread_mutex_unlock(&mut);
  sleep(2);
  }
  printf("thread1 :主函数在等我完成任务吗?\n");
  pthread_exit(NULL);
  //return NULL;
  }
  void * thread2(void *)
  {
  printf("thread2 : I'm thread 2\n");
  for (i = 0; i < MAX; i++)
  {
  printf("thread2 : number = %d\n",number);
  pthread_mutex_lock(&mut);
  number++;
  pthread_mutex_unlock(&mut);
  sleep(3);
  }
  printf("thread2 :主函数在等我完成任务吗?\n");
  pthread_exit(NULL);
  //return NULL;
  }
  void thread_create(void)
  {
  int temp;
  memset(&thread, 0, sizeof(thread)); //comment1
  /*创建线程*/
  if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
  printf("线程1创建失败!\n");
  else
  printf("线程1被创建\n");
  if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
  printf("线程2创建失败");
  else
  printf("线程2被创建\n");
  }
  void thread_wait(void)
  {
  /*等待线程结束*/
  if(thread[0] !=0) { //comment4
  pthread_join(thread[0],NULL);
  printf("线程1已经结束\n");
  }
  if(thread[1] !=0) { //comment5
  pthread_join(thread[1],NULL);
  printf("线程2已经结束\n");
  }
  }
  int main()
  {
  /*用默认属性初始化互斥锁*/
  pthread_mutex_init(&mut,NULL);
  printf("我是主函数哦,我正在创建线程,呵呵\n");
  thread_create();
  printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");
  thread_wait();
  return 0;
  }
  编译此程序:gcc leefeifei.c -lpthread -o leefeifei
  运行结果:
  我是主函数哦,我正在创建线程,呵呵
  线程1被创建
  thread1 : I'm thread 1
  thread1 : number = 0
  线程2被创建
  我是主函数哦,我正在等待线程完成任务阿,呵呵
  thread2 : I'm thread 2
  thread2 : number = 0
  thread1 : number = 2
  thread2 : number = 3
  thread1 : number = 4
  thread2 : number = 5
  thread1 : number = 6
  thread1 : number = 7
  thread2 : number = 8
  thread1 : number = 9
  thread2 : number = 10
  thread1 :主函数在等我完成任务吗?
  线程1已经结束
  thread2 :主函数在等我完成任务吗?
  线程2已经结束
阅读(395) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~