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

河北公司网站建设aspnet网站开发教程

河北公司网站建设,aspnet网站开发教程,如果做网站需要多少钱,昆明网站建设在河科技1、概念 1.1 定义 自旋锁(Spinlock)是一种特殊的锁机制,当线程尝试获取锁而锁不可用时,线程会进入忙等待(即循环检查锁是否可用),而不是进入睡眠状态。这种机制适用于锁持有时间非常短的场景&…

1、概念

1.1 定义

自旋锁(Spinlock)是一种特殊的锁机制,当线程尝试获取锁而锁不可用时,线程会进入忙等待(即循环检查锁是否可用),而不是进入睡眠状态。这种机制适用于锁持有时间非常短的场景,因为它避免了线程上下文切换的开销。然而,如果锁持有时间较长,自旋锁可能会导致CPU资源的浪费。

1.2 特点

  • 忙等待:自旋锁会在获取锁时不断循环检查锁的状态,直到获取到锁为止。这种忙等待的方式可以减少线程切换的开销,适用于对锁的占用时间较短的情况。
  • 无阻塞:自旋锁不会将线程阻塞,因此适用于对锁的占用时间较短、竞争不激烈的情况。
  • 适用于多核CPU:自旋锁在多核CPU上效果更好,因为在一个核上的线程忙等待时,其他核上的线程可以继续执行。

1.3 应用场景

  • 短期占用:适用于对锁的占用时间较短的情况,避免线程切换的开销。
  • 低竞争:适用于竞争不激烈的情况,避免线程频繁地阻塞和唤醒。
  • 多核CPU:在多核CPU上,自旋锁的效率更高,因为可以充分利用多核并行执行的优势

2、常用接口

2.1 pthread_spin_init

初始化一个自旋锁。

pthread_spin_init(pthread_spinlock_t *lock, int pshared)
  • 入参
    • lock:指向要初始化的自旋锁的指针。
    • pshared:指定锁的共享属性,通常设置为PTHREAD_PROCESS_PRIVATE。
  • 返回值:若成功,返回0;否则返回错误码。

2.2 pthread_spin_lock

获取自旋锁。

pthread_spin_lock(pthread_spinlock_t *lock)
  • 入参
    • lock:指向要获取的自旋锁的指针。
  • 返回值:若成功,返回0;否则返回错误码。

2.3 pthread_spin_trylock

尝试获取自旋锁,如果锁已被其他线程占用,则立即返回。

pthread_spin_trylock(pthread_spinlock_t *lock)
  • 入参
    • lock:指向要尝试获取的自旋锁的指针。
  • 返回值:若成功获取锁,返回0;若锁已被占用,返回EBUSY;其他情况返回错误码。

2.4 pthread_spin_unlock

释放自旋锁。

pthread_spin_unlock(pthread_spinlock_t *lock)
  • 作用:释放自旋锁。
  • 入参
    • lock:指向要释放的自旋锁的指针。
  • 返回值:若成功,返回0;否则返回错误码。

2.5 pthread_spin_destroy

销毁一个已经初始化的自旋锁。

pthread_spin_destroy(pthread_spinlock_t *lock)
  • 作用:销毁一个已经初始化的自旋锁。
  • 入参
    • lock:指向要销毁的自旋锁的指针。
  • 返回值:若成功,返回0;否则返回错误码。

3、编程测试

分别测试使用自旋锁和不使用自旋锁操作全局变量的情况。定义一个全局共享变量shared_resource和两个线程都将访问并修改它。使用pthread_spinlock_t类型的变量spinlock作为自旋锁。在thread_function函数中,线程首先尝试使用pthread_spin_lock函数获取自旋锁,如果锁不可用,线程会忙等待直到锁可用。获取锁后,线程访问并修改共享资源,然后释放锁,使用pthread_spin_unlock函数。最后,主线程等待两个工作线程完成,并输出最终的共享资源值。

先将自旋锁代码注释掉,测试程序:

#include <stdio.h>  
#include <stdlib.h>  
#include <pthread.h>  // 全局共享变量  
int shared_resource = 0;  // 自旋锁  
pthread_spinlock_t spinlock;  // 线程函数  
void* thread_function(void* arg) 
{  int thread_id = *(int*)arg;  // 尝试获取自旋锁  // pthread_spin_lock(&spinlock);  // 访问共享资源  for (int i = 0; i < 100000; i++) {  shared_resource += 1;  }  printf("Thread %d finished, shared_resource = %d\n", thread_id, shared_resource);  // 释放自旋锁  // pthread_spin_unlock(&spinlock);  return NULL;  
}  int main() 
{  // 初始化自旋锁  if (pthread_spin_init(&spinlock, 0) != 0) {  printf("Spinlock initialization failed\n");  return 1;  }  // 创建两个线程  pthread_t thread1, thread2;  int thread1_id = 1, thread2_id = 2;  if (pthread_create(&thread1, NULL, thread_function, &thread1_id) != 0) {  printf("Thread 1 creation failed\n");  return 1;  }  if (pthread_create(&thread2, NULL, thread_function, &thread2_id) != 0) {  printf("Thread 2 creation failed\n");  return 1;  }  // 等待线程结束  pthread_join(thread1, NULL);  pthread_join(thread2, NULL);  // 销毁自旋锁  pthread_spin_destroy(&spinlock);  printf("Final shared_resource value: %d\n", shared_resource);  return 0;  
}

测试结果异常,结果不可控:

去掉注释测试自旋锁功能:

#include <stdio.h>  
#include <stdlib.h>  
#include <pthread.h>  // 全局共享变量  
int shared_resource = 0;  // 自旋锁  
pthread_spinlock_t spinlock;  // 线程函数  
void* thread_function(void* arg) 
{  int thread_id = *(int*)arg;  // 尝试获取自旋锁  pthread_spin_lock(&spinlock);  // 访问共享资源  for (int i = 0; i < 100000; i++) {  shared_resource += 1;  }  printf("Thread %d finished, shared_resource = %d\n", thread_id, shared_resource);  // 释放自旋锁  pthread_spin_unlock(&spinlock);  return NULL;  
}  int main() 
{  // 初始化自旋锁  if (pthread_spin_init(&spinlock, 0) != 0) {  printf("Spinlock initialization failed\n");  return 1;  }  // 创建两个线程  pthread_t thread1, thread2;  int thread1_id = 1, thread2_id = 2;  if (pthread_create(&thread1, NULL, thread_function, &thread1_id) != 0) {  printf("Thread 1 creation failed\n");  return 1;  }  if (pthread_create(&thread2, NULL, thread_function, &thread2_id) != 0) {  printf("Thread 2 creation failed\n");  return 1;  }  // 等待线程结束  pthread_join(thread1, NULL);  pthread_join(thread2, NULL);  // 销毁自旋锁  pthread_spin_destroy(&spinlock);  printf("Final shared_resource value: %d\n", shared_resource);  return 0;  
}

测试结果正常:

4、总结

本文讲解了Linux线程同步中使用的自旋锁的定义和应用场景,列举了编程中常用的接口,并编写测试用例进行测试。

http://www.yayakq.cn/news/381311/

相关文章:

  • 晋城建设网站南京公司做网站
  • 广西建设厅官方网站文件通知wordpress 本地写文章
  • 怎么给网站加代码产品网站别人是如何做优化的
  • 网站的不同类电信网站备案查询系统
  • 比较好网站制作公司自己怎么做短视频网站
  • 如何做拼多多商城官网站ueditor编辑器wordpress
  • 网站设计活动主题百度官网入口
  • 小程序网站建设利于优化的网站
  • 自助建站系统破解版深圳手机网站开发
  • 电脑可以做网站服务器么淄博微信小程序代理
  • 宜昌网站建设选择宜昌慧享互动dede网站百度统计怎么做
  • 建个网站需要多少钱?龙采做网站要多少钱
  • 做公司网站 哪个程序用的多如何做泛解析网站
  • 做个网站需要多少钱wordpress用户注册收不到邮件
  • 关于建设网站的书本微信公众号开发者中心在哪里
  • 室内设计师网名专用站长工具seo查询
  • 可以做描文本的网站泰安集团网站建设地点
  • 域名怎么建网站贴吧高级搜索
  • 网站关键词seo优化怎么做推广怎么做
  • 住房和城乡建设主管部门网站品牌网站建设怎么样
  • 网站开发下载那个泰安新闻联播
  • 海米云网站建设wampserver 架设wordpress 主题错误
  • 网站建设做的好的公司2021年中国关键词
  • 做学校网站的内容桂林论坛网网站电话
  • 大连建站程序刚刚刚刚刚刚好痛
  • 安徽省两学一做网站专栏营销型网站规划
  • 折800网站模板泊头哪里建网站呢
  • 网站建设谈单情景对话网站备案资料
  • 免费网站制作平台下载如何制造一个网站
  • 企业网站建设解决方案报告论文官方网站下载微博