多线程的共享资源的解决方案
来源:互联网 发布:淘宝visa卡怎样付款 编辑:程序博客网 时间:2024/05/17 22:48
线程:是轻量级的进程,也是程序执行的最小单位。
线程的特点:共享进程的内存空间,对于操作系统而言,进程和线程都会参与系统的统一调度,同样用task_struct来描述线程。
注意事项:由于多线程是通过第三方的线程库来实现的,所以在LINUX 的gcc 编译器下要这样编译,如要编译thread1.c这个文件,命令如下:gcc thread1.c -o thread1 -lpthread 后面-lpthread就是所要链接的第三方的线程库。
下面是关于线程的一些函数
创建线程的函数
#include <pthread.h>//头文件 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void (*start_routine) (void *), void *arg);
函数参数:thread:创建的线程
attr 指定线程的属性
arg 传递给线程执行的函数参数
routine:线程执行的函数
返回值:成功0 失败 -1
等待线程结束的函数
int pthread_join(pthread_t thread, void **retval);
线程退出函数原型
void pthread_exit(void *retval);
今天,我使用互斥锁来保证共享数据的完整性,源代码thread-unlock.c
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#define N 20void *threadFunc(void *argc){ char *p =(char *)argc; static char buf[N]={0}; int i=0; while(*p!='\0') { buf[i]=*p; i++; p++; sleep(1); } puts(buf); pthread_exit(NULL);}int main(){ char str1[]="xldnzlh"; char str2[]="6663217"; //创建两个线程 pthread_t th1,th2; pthread_create(&th1,NULL,threadFunc,str1); pthread_create(&th2,NULL,threadFunc,str2);//等待线程 pthread_join(th1,NULL); pthread_join(th2,NULL); return 0;}
出现资源竞争情况
但是加上互斥锁之后就可以实现资源的完整性,一个线程执行完之后,另一个才可以执行. 源代码 thread-lock.c 如下
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#define N 20pthread_mutex_t mutex;void *threadFunc(void *argc){ char *p =(char *)argc; static char buf[N]={0}; int i=0; // 申请锁 pthread_mutex_lock(&mutex); while(*p!='\0') { buf[i]=*p; i++; p++; sleep(1); } puts(buf); //释放锁 pthread_mutex_unlock(&mutex); pthread_exit(NULL);}int main(){//初始化互斥锁 pthread_mutex_init(&mutex,NULL); char str1[]="xldnzlh"; char str2[]="6663217"; pthread_t th1,th2; pthread_create(&th1,NULL,threadFunc,str1); pthread_create(&th2,NULL,threadFunc,str2); pthread_join(th1,NULL); pthread_join(th2,NULL); return 0;}
测试结果如下
接着我们也可以使用一个信号量实现互斥锁的功能。
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <semaphore.h>#define N 20sem_t sem1;void *threadFunc(void *argc){ char *p =(char *)argc; static char buf[N]={0}; int i=0; // p操作 sem_wait(&sem1); while(*p!='\0') { buf[i]=*p; i++; p++; sleep(1); } puts(buf); //v操作 sem_post(&sem1); pthread_exit(NULL);}int main(){ //初始化一个信号量 sem_init(&sem1,0,1); char str1[]="xldnzlh"; char str2[]="6663217"; pthread_t th1,th2; pthread_create(&th1,NULL,threadFunc,str1); pthread_create(&th2,NULL,threadFunc,str2); pthread_join(th1,NULL); pthread_join(th2,NULL); return 0;}
具体的测试结果如下所示:
以后会持续更新,写的不好请大家见谅,关注我一下,一块交流。
- 多线程的共享资源的解决方案
- 多线程对共享资源的访问
- 关于多线程共享资源的方式
- 多线程间的共享资源有哪些
- 多线程 线程的创建方式和多线程共享资源
- 多线程共享资源
- 多线程共享资源
- 谢谢 共享资源的朋友
- 一款共享资源的软件
- 共享资源的公平性研究
- 线程共享资源的问题
- 我的共享资源
- 共享资源的简单示例
- 关于添加共享资源的方法
- 高效的读写锁-共享资源
- 两种方式实现多线程共享资源(典型的售票例子)
- 多线程共享资源参考
- 多线程共享资源冲突
- Java8系列--Java Stream入门篇(什么是Stream)
- 华科15_3
- java方法参数传递方式
- C++输入输出总结
- HDU 3342&&UVA10305&&考试day3 T1
- 多线程的共享资源的解决方案
- linux 安装mysql数据库——tar.gz包解压安装法
- Eclipse之task任务提醒
- JAVA中的this
- Quartz入门指南
- 网络原理,以及对VMware Workstation虚拟网络VMnet0、VMnet1、VMnet8的图解
- Bootstrap实现部分网页中常见的页面效果!
- 管理信息系统---第一遍导图
- 数据结构【排序算法】——冒泡排序