旋转锁

原子操作

所谓原子操作是指不会被 线程调度 机制打断的操作;这种操作一旦开始,就一直运行到结束,中间不会有任何 context switch (切 换到另一个线程)。

旋转锁  

	BOOL bUsering = FALSE;
	while(InterlockedExchange((long *)&bUsering, TRUE) == TRUE)
		Sleep(0);

	//绝对安全领域
	InterlockedExchange((long *)&bUsering, FALSE);

绝对安全
需要注意的地方
1 如果  里面的线程的优先级不同  优先级低的有可能饿死
2  因为是循环   cpu使用率过高

因为编译器优化的不同所以引发了另一个问题就是 直接把锁 优化没了  不确定因为编译器本身就是不可理喻的 - -       
volatile   关键字 让他绝对不会被优化
volatile BOOL bUseing = FALSE;


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Unity 3D 密码脚本示例: ``` using System.Collections; using System.Collections.Generic; using UnityEngine; public class Lock : MonoBehaviour { public Transform[] dials; // 数字盘数组 public int[] combination; // 正确的组合数字序列 private int[] current; // 当前输入的数字序列 private int dialIndex; // 当前数字盘的索引 void Start() { current = new int[dials.Length]; } void Update() { float input = Input.GetAxis("Vertical"); if (input > 0) { RotateDial(1); } else if (input < 0) { RotateDial(-1); } if (Input.GetKeyDown(KeyCode.Space)) { CheckCombination(); } } void RotateDial(int direction) { dials[dialIndex].Rotate(Vector3.up * direction * 30); // 每次旋转 30 度 current[dialIndex] = (current[dialIndex] + direction) % 10; // 更新当前数字 if (current[dialIndex] < 0) { current[dialIndex] += 10; } } void CheckCombination() { bool isCorrect = true; for (int i = 0; i < dials.Length; i++) { if (current[i] != combination[i]) { isCorrect = false; break; } } if (isCorrect) { Debug.Log("Correct combination!"); } else { Debug.Log("Incorrect combination."); } } } ``` 该脚本包含了以下功能: 1. 根据输入的垂直轴向旋转数字盘。 2. 检查当前输入的数字序列是否与正确的组合数字序列相匹配。 3. 在正确的组合数字序列中,每个数字盘都有一个对应的数字。 4. 在每次旋转数字盘时,更新当前输入的数字序列。 5. 如果当前输入的数字序列与正确的组合数字序列相匹配,则输出“正确的组合!”;否则输出“不正确的组合。” 注意:这只是一个简单的示例,可以根据需要进行修改和扩展,以满足特定的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值