银 川 外 围 - 银 川 哪 有 外 围

  银 川 外 围 - 银 川 哪 有 外 围 -薇芯【81343628】【81343628】【高端外围资源】【诚信合作,非诚勿扰!】可直接添加 !线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立执行的基本单位。线程自己基本上不拥有系统资源,仅仅拥有一点在执行中不可缺少的资源(如程序计数器,一组寄存器和栈),可是它可与同属一个进程的其它的线程共享进程所拥有的所有资源。
  二、什么时候使用多线程?
  当多个任务能够并行运行时,能够为每一个任务启动一个线程。
  三、线程的创建
  使用pthread_create函数。
  #include<pthread.h>
  银 川 外 围 - 银 川 哪 有 外 围 -薇芯【81343628】【81343628】【高端外围资源】【诚信合作,非诚勿扰!】可直接添加 !int pthread_create (pthread_t *__restrict __newthread,//新创建的线程ID
   __const pthread_attr_t *__restrict __attr,//线程属性
   void *(*__start_routine) (void *),//新创建的线程从start_routine開始运行
   void *__restrict __arg)//运行函数的參数
  返回值:成功-0,失败-返回错误编号,能够用strerror(errno)函数得到错误信息
  四、线程的终止
  三种方式
  线程从运行函数返回,返回值是线程的退出码
  线程被同一进程的其它线程取消
  调用pthread_exit()函数退出。这里不是调用exit,由于线程调用exit函数,会导致线程所在的进程退出。
  一个小样例:
  启动两个线程,一个线程对全局变量num运行加1操作,运行五百次,一个线程对全局变量运行减1操作,相同运行五百次。
  #include <stdio.h>
  #include <stdlib.h>
  #include <pthread.h>
  #include <unistd.h>
  #include <string.h>
  int num=0;
  void *add(void *arg) {//线程运行函数,运行500次加法
  int i = 0,tmp;
  for (; i <500; i++)
  {
  tmp=num+1;
  num=tmp;
  printf("add+1,result is:%d\n",num);
  }
  return ((void *)0);
  }
  void *sub(void *arg)//线程运行函数,运行500次减法
  {
  int i=0,tmp;
  for(;i<500;i++)
  {
  tmp=num-1;
  num=tmp;
  printf("sub-1,result is:%d\n",num);
  }
  return ((void *)0);
  }
  int main(int argc, char** argv) {
  pthread_t tid1,tid2;
  int err;
  void *tret;
  err=pthread_create(&tid1,NULL,add,NULL);//创建线程
  if(err!=0)
  {
  printf("pthread_create error:%s\n",strerror(err));
  exit(-1);
  }
  err=pthread_create(&tid2,NULL,sub,NULL);
  if(err!=0)
  {
  printf("pthread_create error:%s\n",strerror(err));
  exit(-1);
  }
  err=pthread_join(tid1,&tret);//堵塞等待线程id为tid1的线程,直到该线程退出
  if(err!=0)
  {
  printf("can not join with thread1:%s\n",strerror(err));
  exit(-1);
  }
  printf("thread 1 exit code %d\n",(int)tret);
  err=pthread_join(tid2,&tret);
  if(err!=0)
  {
  printf("can not join with thread1:%s\n",strerror(err));
  exit(-1);
  }
  printf("thread 2 exit code %d\n",(int)tret);
  return 0;
  }
  使用g++编译该文件(g++ main.cpp -o main)。此时会报错undefined reference to `pthread_create‘。
  报这个错误的原因是:pthread库不是linux默认的库,所以在编译时候须要指明libpthread.a库。
  解决方法:在编译时,加上-lpthread參数。
  运行结果:
  乍一看,结果是对的,加500次,减500次,最后结果为0。可是细致看全部的输出,你会发现有异样的东西。
  导致这个不和谐出现的原因是,两个线程能够对同一变量进行改动。假如线程1运行tmp=50+1后,被系统中断,此时线程2对num=50运行了减一操作,当线程1恢复,在运行num=tmp=51。而正确结果应为50。所以当多个线程对共享区域进行改动时,应该採用同步的方式。
  五、线程同步
  线程同步的三种方式:
  1、相互排斥量
  相互排斥量用pthread_mutex_t数据类型来表示。
  两种方式初始化,第一种:赋值为常量PTHREAD_MUTEX_INITIALIZER;另外一种,当相互排斥量为动态分配是,使用pthread_mutex_init函数进行初始化,使用pthread_mutex_destroy函数销毁。
  #include<pthread.h>
  int pthread_mutex_init (pthread_mutex_t *__mutex,
   __const pthread_mutexattr_t *__mutexattr);
  int pthread_mutex_destroy (pthread_mutex_t *__mutex);
  返回值:成功-0,失败-错误编号
  加解锁
  加锁调用pthread_mutex_lock,解锁调用pthread_mutex_unlock。
  #include<pthread.h>
  int pthread_mutex_lock (pthread_mutex_t *__mutex);
  int pthread_mutex_unlock (pthread_mutex_t *__mutex);
  银 川 外 围 - 银 川 哪 有 外 围 -薇芯【81343628】【81343628】【高端外围资源】【诚信合作,非诚勿扰!】可直接添加 !使用相互排斥量改动上一个程序(改动部分用红色标出):

猜你喜欢

转载自www.cnblogs.com/wwyyff80/p/12784088.html