This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
我正在使用供应商提供的 DHCP 库在 W5500芯片上实现 DHCP、但我担心该库可能会被错误地同步。 该库使用易失性计数器来检测超时、并假设有两个线程:一个线程重置计数器并检查计数器是否已过期、另一个线程使其递增。 声明如下:
volatile uint32_t dhcp_tick_1s = 0; // unit 1 second
这里是正在使用的计数器:
/* Reset the DHCP timeout count and retry count. */ void reset_DHCP_timeout(void) { dhcp_tick_1s = 0; dhcp_tick_next = DHCP_WAIT_TIME; dhcp_retry_count = 0; } void DHCP_time_handler(void) { dhcp_tick_1s++; }
您好!
如您所读、编译器不支持原子操作。dhcp_tick_1s++将成为一个读取-修改-写入操作、其中处理器 必须 从存储器中读取变量(因为变量是易失性的)。 读取后、处理器需要在其中添加一个、然后写回存储器。 读取-修改-写入可以在中间中断。 您需要实现信标等某种类型的同步、使其成为原子。 您还可以尝试禁用中断、以便在 您处于 RESET_DHCP_TIMEOUT 的中间时不调用 DHCP_TIME_handler。