您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > Linux系统编程(第八章)线程
第八章:线程目标:本章旨在向学员介绍linux操作系统下线程的使用:1)了解linux系统下线程与进程的区别2)掌握线程相关的编程方法3)掌握线程间通信同步的机制时间:3.5学时教学方法:讲授PPT、实例练习8.1什么是线程?定义定义一个程序中的多个执行路线就叫线程(thread)线程是一个进程内部的控制序列,进程至少有一个执行线程不同不同调用fork创建的进程拥有自己的变量和PID,时间调度也独立,进程中创建线程时,新的线程拥有自己的栈,与它的创建者共享全局变量、文件描述符、信号句柄等资源特点特点线程执行开销小,但不利于资源的管理和保护,适合于在SMP机器上运行8.2线程•pthread_create()函数创建一个新线程,类似于创建新进程的fork函数参数thread:线程创建时,这个指针指向变量中被写入一个标识符,标识符来引用新线程参数attr:用于设置线程的属性参数start_routine:指定线程将要执行的函数参数arg:要执行函数传递的参数#includepthread.hintpthread_create(pthread_t*thread,pthread_attr_t*attr,void*(*start_routine)(void*),void*arg);8.2线程•线程终止函数pthread_exit#includepthread.hvoidpthread_exit(void*retval);8.2线程•收集线程函数pthread_join作用等价于进程中用来收集子进程信息的wait函数。参数th:指定将要等待的线程参数thread_return:指向线程的返回值#includepthread.hintpthread_join(pthread_tth,void**thread_return);8.2线程实验:简单的线程程序#includepthread.hvoid*thread_function(void*arg);charmessage[]=“HelloWorld”;intmain(){intres;pthread_ta_thread;void*thread_result;res=pthread_create(&a_thread,NULL,thread_function,(void*)message);if(res!=0){perror(“Threadcreationfailed”);exit(EXIT_FAILURE);}printf(“Waitingforthreadtofinish…\n”);res=pthread_join(a_thread,&thread_result);if(res!=0){perror(“Threadjoinfiled”);exit(EXIT_FAILURE);}printf(“Threadjoined,itreturned%s\n”,(char*)thread_result);printf(“Messageisnow%s\n”,message);exit(EXIT_SUCCESS);}void*thread_function(void*arg){printf(“thread_functionisrunning.Argumentwas%s\n”,(char*)arg);sleep(3);strcpy(message,“Bye!”);pthread_exit(“ThankyoufortheCPUtime”);}8.2线程•练习:编写一个程序,至少创建2个线程,两个线程都循环执行,一个线程每隔1秒输出我是线程1,另一个线程每个1秒输出我是线程28.3线程的同步机制•信号量与进程间通信机制类似,但仅用于线程间同步操作过程中•互斥量信号量的另一种应用,线程同步机制的一种,某个线程先取得资源后,后访问资源的线程会被阻塞8.3.1线程信号量•信号量创建函数sem_init参数sem:初始化的信号量对象参数pshared:控制信号量的类型#includesemaphore.hintsem_init(sem_t*sem,intpshared,unsignedintvalue);8.3.1线程信号量•信号量控制函数sem_wait和sem_post参数sem:指向sem_init初始化的信号量的指针参数#includesemaphore.hintsem_wait(sem_t*sem);intsem_post(sem_t*sem);8.3.1线程信号量•信号量清理函数sem_destroy#includesemaphore.hintsem_destroy(sem_t*sem);8.3.1线程信号量练习:线程信号量#includestdio.h#includeunistd.h#includestdlib.h#includepthread.h#includesemaphore.h#includesignal.hsem_tsem;void*thread_function(void*arg);voidsendsem(){sem_post(&sem);}charmessage[]=HelloWorld;intmain(){intres;pthread_ta_thread;void*thread_result;sem_init(&sem,0,0);signal(SIGINT,sendsem);res=pthread_create(&a_thread,NULL,thread_function,(void*)message);if(res!=0){perror(Threadcreationfailed);exit(EXIT_FAILURE);}printf(WaitingforSEMfromSIGNAL...\n);res=pthread_join(a_thread,&thread_result);if(res!=0){perror(“Threadjoinfailed”);exit(EXIT_FAILURE);}printf(“Threadjoined\n”);exit(EXIT_FAILURE);}void*thread_function(void*arg){sem_wait(&sem);printf(“thread_functionisrunning.Argumentwas%s\n”,(char*)arg);sleep(1);pthread_exit(NULL);}8.3.2线程互斥量•初始化互斥量互斥函数使用的方法与信号量类似#includepthread.hintpthread_mutex_init(pthread_mutex_t*mutex,constpthread_mutexattr_t*mutexattr);8.3.2线程互斥量•控制互斥量函数pthread_mutex_lock和pthread_mutex_unlock#includepthread.hintpthread_mutex_lock(pthread_mutex_t*mutex);intpthread_mutex_unlock(pthread_mutex_t*mutex);8.3.2线程互斥量•互斥量清理函数pthread_mutex_destroy#includepthread.hintpthread_mutex_destroy(pthread_mutex_t*mutex);8.3.2线程互斥量练习:线程互斥量intcount=0;pthread_mutex_tcount_lock=PTHREAD_MUTEX_INITIALIZER;void*thread_function(void*arg);char*message1=I'mthread1;char*message2=I'mthread2;intmain(){intres;pthread_ta_thread;pthread_tb_thread;void*thread_result;res=pthread_create(&a_thread,NULL,thread_function,(void*)message1);if(res!=0){perror(Threadcreationfailed);exit(EXIT_FAILURE);}res=pthread_create(&b_thread,NULL,thread_function,(void*)message2);if(res!=0){perror(Threadcreationfailed);exit(EXIT_FAILURE);}printf(Waitingforthreadtofinish...\n);res=pthread_join(a_thread,&thread_result);if(res!=0){perror(Threadjoinfailed);exit(EXIT_FAILURE);}printf(Waitingforthreadtofinish...\n);res=pthread_join(b_thread,&thread_result);if(res!=0){perror(Threadjoinfailed);exit(EXIT_FAILURE);}printf(Threadjoined\n);exit(EXIT_FAILURE);}void*thread_function(void*arg){printf(thread_functionisrunning.Argumentwas%s\n,(char*)arg);pthread_mutex_lock(&count_lock);count++;sleep(1);printf(countis%d\n,count);pthread_mutex_unlock(&count_lock);pthread_exit(NULL);}8.3.3线程控制•线程初始化属性函数pthread_attr_init#includepthread.hintpthread_attr_init(pthread_attr_t*attr);8.3.3线程控制•线程属性修改函数,设置线程为独立线程•attr参数:输出线程属性,在pthread_create被调用•detachstate参数:PTHREAD_CREATE_DETACHED,使线程成为独立线程,不需要主线程调用pthread_join进行子线程的资源回收。#includepthread.hintpthread_attr_setdetachstate(pthread_attr_t*attr,intdetachstate);8.3.3线程控制练习:设置脱离状态属性#includestdio.h#includeunistd.h#includestdlib.h#includepthread.hvoid*thread_function(void*arg);charmessage[]=“HelloWorld”;intthread_finished=0;intmain(){intres;pthread_ta_thread;pthread_attr_tthread_attr;res=pthread_attr_init(&thread_attr);if(res!=0){perror(“Attributecreationfailed”);exit(EXIT_FAILURE);}res=pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED);if(res!=0){perror(“Settingdetachedattributefailed”);exit(EXIT_FAILURE);}res=pthread_create(&a_thread,&thread_attr,thread_functi
本文标题:Linux系统编程(第八章)线程
链接地址:https://www.777doc.com/doc-5530167 .html