当前位置: 首页 > news >正文

手机论坛网站源码自己想做一个网站

手机论坛网站源码,自己想做一个网站,网站软文得特点,潍坊住房公积金管理中心文章目录 1、为什么要用互斥锁2、互斥锁怎么用3、为什么要用条件变量4、互斥锁和条件变量如何配合使用5、互斥锁和条件变量的常见用法 参考资料#xff1a;https://blog.csdn.net/m0_53539646/article/details/115509348 1、为什么要用互斥锁 为了使各线程能够有序地访问公共… 文章目录 1、为什么要用互斥锁2、互斥锁怎么用3、为什么要用条件变量4、互斥锁和条件变量如何配合使用5、互斥锁和条件变量的常见用法 参考资料https://blog.csdn.net/m0_53539646/article/details/115509348 1、为什么要用互斥锁 为了使各线程能够有序地访问公共资源。例如有一个全局变量g_count有三个线程thread_fun_1、thread_fun_2、thread_fun_3三个线程都要对g_count写操作。在不加锁的情况下当thread_fun_1正在写数据时thread_fun_2和thread_fun_3也可能会进行写操作这就会导致程序不符合我们的预期结果。而加锁的目的就是保证各线程能够按顺序访问公共资源就好比我们排队WC一样一个人解决完下个人才能解决。不多说直接看下面的例子 #include stdio.h #include unistd.h #include stdlib.h #include string.h #include pthread.hstatic int g_count 0; //共享数据static void *thread_fun_1(void *data) {for(int i0; i10000; i){g_count;}printf(%s g_count: %d\n, __func__, g_count); }static void *thread_fun_2(void *data) {for(int i0; i10000; i){g_count;}printf(%s g_count: %d\n, __func__, g_count); }static void *thread_fun_3(void *data) {for(int i0; i10000; i){g_count;}printf(%s g_count: %d\n, __func__, g_count); }int main(int argc, char const *argv[]) {pthread_t pid[3]; //创建3个线程pthread_create(pid[0], NULL, thread_fun_1, NULL);pthread_create(pid[1], NULL, thread_fun_2, NULL);pthread_create(pid[2], NULL, thread_fun_3, NULL);//等待三个线程结束pthread_join(pid[0], NULL);pthread_join(pid[1], NULL);pthread_join(pid[2], NULL);return 0; }运行结果 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ gcc threadtest1.c -pthread userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ ./a.out thread_fun_1 g_count: 10000 thread_fun_2 g_count: 20000 thread_fun_3 g_count: 30000 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ ./a.out thread_fun_1 g_count: 10000 thread_fun_2 g_count: 20437 thread_fun_3 g_count: 28812 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ 例程解析 上面的例程创建了3个线程每个线程都对全局变量g_count加10000次然后打印g_count的值。从运行结果很容易看出同样的程序两次运行的结果不一致这就是线程无序访问公共资源的原因。2、互斥锁怎么用 说完为什么要用互斥锁接下来就该说下怎么用了步骤如下 // 1、包含pthread.h头文件 #include pthread.h// 2、互斥锁的声明 static pthread_mutex_t g_mutex_lock//3、互斥锁的初始化 pthread_mutex_init(g_mutex_lock, NULL);// 4、锁定互斥锁 pthread_mutex_lock(g_mutex_lock);// 5、执行对共享资源的操作// 6、解锁互斥锁 pthread_mutex_unlock(g_mutex_lock);// 7、销毁互斥锁 pthread_mutex_destroy(g_mutex_lock);接着上面的例子写一个带互斥锁的程序 #include stdio.h #include unistd.h #include stdlib.h #include string.h #include pthread.hstatic int g_count 0; //共享数据 static pthread_mutex_t g_mutex_lock;static void *thread_fun_1(void *data) {pthread_mutex_lock(g_mutex_lock); //上锁for(int i0; i10000; i){g_count;}printf(%s g_count: %d\n, __func__, g_count);pthread_mutex_unlock(g_mutex_lock); //解锁 }static void *thread_fun_2(void *data) {pthread_mutex_lock(g_mutex_lock); //上锁for(int i0; i10000; i){g_count;}printf(%s g_count: %d\n, __func__, g_count);pthread_mutex_unlock(g_mutex_lock); //解锁 }static void *thread_fun_3(void *data) {pthread_mutex_lock(g_mutex_lock); //上锁for(int i0; i10000; i){g_count;}printf(%s g_count: %d\n, __func__, g_count);pthread_mutex_unlock(g_mutex_lock); //解锁 }int main(int argc, char const *argv[]) {int ret;pthread_t pid[3];ret pthread_mutex_init(g_mutex_lock, NULL);if (ret ! 0) {printf(mutex init failed\n);return -1;}pthread_create(pid[0], NULL, thread_fun_1, NULL);pthread_create(pid[1], NULL, thread_fun_2, NULL);pthread_create(pid[2], NULL, thread_fun_3, NULL);pthread_join(pid[0], NULL);pthread_join(pid[1], NULL);pthread_join(pid[2], NULL);pthread_mutex_destroy(g_mutex_lock); } 运行结果 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ gcc threadtest2.c -pthread userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ ./a.out thread_fun_1 g_count: 10000 thread_fun_2 g_count: 20000 thread_fun_3 g_count: 30000 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ ./a.out thread_fun_1 g_count: 10000 thread_fun_2 g_count: 20000 thread_fun_3 g_count: 30000 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ ./a.out thread_fun_1 g_count: 10000 thread_fun_2 g_count: 20000 thread_fun_3 g_count: 30000 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ 从上面的结果可以看到程序多次运行执行结果都是一样的 3、为什么要用条件变量 相信大家到这里会有一个疑问既然互斥锁都能保证程序有序访问了为什么还要使用条件变量呢我们看下面的例子下面代码创建了thread_fun_1和thread_fun_2两个线程thread_fun_2中对g_count全局变量加100次thread_fun_1中判断全局变量的值大于0才执行。按照我们的理解两个线程都能正常运行退出才对但实际运行结果并不是这样。 #include stdio.h #include unistd.h #include stdlib.h #include string.h #include pthread.hstatic int g_count 0; //共享数据 static pthread_mutex_t g_mutex_lock;static void *thread_fun_1(void *data) {pthread_mutex_lock(g_mutex_lock); //上锁printf(%s g_count: %d\n, __func__, g_count);while(0 g_count){//do somethingprintf(%s run ok,g_count: %d\n, __func__, g_count);pthread_mutex_unlock(g_mutex_lock); //解锁break;} }static void *thread_fun_2(void *data) {pthread_mutex_lock(g_mutex_lock); //上锁for(int i0; i100; i){g_count;}printf(%s g_count: %d\n, __func__, g_count);pthread_mutex_unlock(g_mutex_lock); //解锁}int main(int argc, char const *argv[]) {int ret;pthread_t pid[3];ret pthread_mutex_init(g_mutex_lock, NULL);if (ret ! 0) {printf(mutex init failed\n);return -1;}pthread_create(pid[0], NULL, thread_fun_1, NULL);pthread_create(pid[1], NULL, thread_fun_2, NULL);pthread_join(pid[0], NULL);pthread_join(pid[1], NULL);pthread_mutex_destroy(g_mutex_lock); } 运行结果可以看到线程thread_fun_1打印了一串日之后就没有其他打印了程序像是阻塞了一样。其实这里是进入了死锁g_count一开始初始化为0当 thread_fun_1进入临界区时其他线程不能进入临界区意味着 Bthread_fun_2没有机会去修改 g_count g_count的值一直为 0不满足Athread_fun_1继续执行的条件g_count 0Athread_fun_1只能一直等待。 又因为使用了互斥锁当 thread_fun_1进入临界区时其他线程不能进入临界区意味着 thread_fun_2没有机会去修改 g_count g_count 的值一直为 0不满足thread_fun_1继续执行的条件g_count 0。最终结果thread_fun_1只能一直等待thread_fun_2不能执行导致整个程序不能正常运行。这时候就需要使用条件变量了。 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ gcc threadtest3.c -pthread userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ ./a.out thread_fun_1 g_count: 0 4、互斥锁和条件变量如何配合使用 使用步骤 // 1、包含pthread.h头文件 #include pthread.h// 2、条件变量声明 pthread_cond_t g_cond ; //3、条件初始化 pthread_cond_init(g_cond, NULL) ;// 4、/* 令一个线程A等待在条件变量上 */ pthread_cond_wait(g_cond, g_mutex_lock) ;// 5、线程B执行对共享资源的操作// 6、线程B通知等待在条件变量上的线程A,线程通知函数有2个pthread_cond_broadcast是通知所有线程 // pthread_cond_signal是至少通知一个线程一般使用pthread_cond_broadcast函数 pthread_cond_broadcast(g_cond) ; pthread_cond_signal(g_cond);// 7、销毁条件变量 pthread_cond_destroy( g_cond ) ;我们通过条件变量解决上面的问题 #include stdio.h #include unistd.h #include stdlib.h #include string.h #include pthread.hstatic int g_count 0; //共享数据 static pthread_mutex_t g_mutex_lock;//互斥锁 pthread_cond_t g_cond ; //条件变量static void *thread_fun_1(void *data) {pthread_mutex_lock(g_mutex_lock); //上锁printf(%s g_count: %d\n, __func__, g_count);pthread_cond_wait(g_cond, g_mutex_lock) ;while(0 g_count){//do somethingprintf(%s run ok,g_count: %d\n, __func__, g_count);pthread_mutex_unlock(g_mutex_lock); //解锁break;} }static void *thread_fun_2(void *data) {pthread_mutex_lock(g_mutex_lock); //上锁for(int i0; i100; i){g_count;pthread_cond_broadcast(g_cond) ;}printf(%s g_count: %d\n, __func__, g_count);pthread_mutex_unlock(g_mutex_lock); //解锁}int main(int argc, char const *argv[]) {int ret;pthread_t pid[3];pthread_cond_init(g_cond, NULL) ;ret pthread_mutex_init(g_mutex_lock, NULL);if (ret ! 0) {printf(mutex init failed\n);return -1;}pthread_create(pid[0], NULL, thread_fun_1, NULL);pthread_create(pid[1], NULL, thread_fun_2, NULL);pthread_join(pid[0], NULL);pthread_join(pid[1], NULL);pthread_cond_destroy( g_cond ) ;pthread_mutex_destroy(g_mutex_lock); } 运行结果 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ gcc threadtest4.c -pthread userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ ./a.out thread_fun_1 g_count: 0 thread_fun_2 g_count: 100 thread_fun_1 run ok,g_count: 100 userroot:/mnt/hgfs/UbuntuRK3568/04_ThreadLockTest$ 5、互斥锁和条件变量的常见用法 一般做项目的时候如果我们希望多个线程使用同一资源我们一般会将这部分资源打包成一个结构体通过结构体定义一个全局变量供各线程使用。所以互斥锁和条件变量一般也是和结构体一起用的如下面的例子加锁、解锁还有初始化跟上面的举的例子一样放在结构体表示对该结构体的数据进行加锁。 typedef struct {int a;char buf[32];/* ... */pthread_mutex_t mutex_test;pthread_cond_t cond_test; } test;就写这么多吧如果哪里有问题欢迎大家指正。
http://www.hkea.cn/news/14538768/

相关文章:

  • 网站建设制作视频wordpress七牛云加速
  • 医疗器械网站制作wordpress博客免费主题
  • 网站建设公司怎么寻找客户呢苏州物联网app开发公司
  • 网站二级栏目数量网站质量
  • asp做的静态网站卡不卡快速网站制作
  • 温州平阳县网站建设兼职html写的网页怎么在手机上看
  • 对网站建设的意见和建议营销型网站 典型案例
  • 西安网站设计制作一般多少钱17. 整个网站建设中的关键是
  • 东莞网站建设哪家专业网站备案名称的影响
  • 全屏网站 代码网站建设的市场调研
  • 网站类别标签文本江苏建设人才证书查询
  • 新乡网站建设网站开发有哪些模块
  • 开发网站wordpress主题:yusi v2.0
  • 中间商网站怎么做亚马逊查关键词搜索量的工具
  • 建设银行网站设计的优点美妆网站开发规划书
  • 阿坝州建设局网站刘志彬新冠止咳药物有哪几种
  • 网站风格设计原则青岛中小企业网站制作
  • 新民专业网站开发公司怎样查询江西省城乡建设厅网站
  • 服装公司网站建设方案太湖云建站网站建设
  • 网站域名价值查询自建网站该页无法显示
  • 做ppt配图好用的网站档案馆网站机房建设
  • 大型网站建设公司制作网站久久建筑网会员登陆中心
  • 展示系统 网站模板智慧新零售系统
  • 网站栏目内容和功能深圳全网推广推荐
  • wordpress站点地址写错免费的微网站
  • 专业网站设计建设服务广州网站推广费用
  • it培训机构学校班级优化大师官方免费下载
  • 苏州专业高端网站建设百度推广送企业网站吗
  • 个人网站免费搭建南昌做网站需要多少钱
  • 网站建设经费方案百度关键词搜索查询