Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Program C Ansi Programming Embedded Systems in C and C++ phần 6 potx
MIỄN PHÍ
Số trang
13
Kích thước
89.9 KB
Định dạng
PDF
Lượt xem
1877

Program C Ansi Programming Embedded Systems in C and C++ phần 6 potx

Nội dung xem thử

Mô tả chi tiết

state as volatile. Doing so would prevent the compiler from incorrectly assuming that the timer's state is either done

or not done and optimizing away the while loop.[3]

[3] A word of caution about waitfor : this implementation spins its wheels waiting for the software timer to change to

the done state. This technique is called busy-waiting, and it is neither elegant nor an efficient use of the processor. In

Chapter 8, we'll see how the introduction of an operating system allows us to improve upon this implementation.

The final method of the Timer class is used to cancel a running timer. This is easy to implement because we need

only remove the timer from the timer list and change its state to Idle. The code that actually does this is shown here:

/**********************************************************************

*

* Method: cancel()

*

* Description: Stop a running timer.

*

* Notes:

*

* Returns: None defined.

*

**********************************************************************/

void

Timer::cancel(void)

{

//

// Remove the timer from the timer list.

//

if (state == Active)

{

timerList.remove(this);

}

//

// Reset the timer's state.

//

state = Idle;

} /* cancel() */

Of course, there is also a destructor for the Timer class, though I won't show the code here. Suffice it to say that it

just checks to see if the software timer is active and, if so, removes it from the timer list. This prevents a periodic

timer that has gone out of scope from remaining in the timer list indefinitely and any pointers to the "dead" timer

from remaining in the system.

For completeness, it might be nice to add a public method, perhaps called poll, that allows users of the Timer class

to test the state of a software timer without blocking. In the interest of space, I have left this out of my

implementation, but it would be easy to add such a routine. It need only return the current value of the comparison

state == Done. However, in order to do this, some technique would need to be devised to restart periodic timers for

which waitfor is never called.

Tải ngay đi em, còn do dự, trời tối mất!