This is the sixth day of my participation in the August Text Challenge.More challenges in August

glib

Timers are used in various scenarios to execute scheduled tasks. Having looked at the TBOX timer, let’s look at the Glib timer.

Timer

GTimer records a start time and counts the number of microseconds that have passed since then. This is done a little differently on different platforms and can be difficult to get exactly right, so GTimer provides a portable/convenient setup.

/**
 * GTimer:
 *
 * Opaque datatype that records a start time.
 **/
struct _GTimer
{
  guint64 start;
  guint64 end;
​
  guint active : 1;
};
Copy the code

Because it contains too many functions, so combined with the analysis, only the following content is investigated.

g_timer_new ()

GTimer *
g_timer_new (void);
Copy the code

Create a new timer and start the timer (that is, implicitly call g_timer_start () for you).

g_timer_start ()

void
g_timer_start (GTimer *timer);
Copy the code

Mark a start time so that future calls to g_timer_Elapsed () will report the time since g_timer_start () was called. G_ timer_new () automatically marks the start time, so there is no need to call g_ timer_start () immediately after the timer is created.

g_timer_stop ()

void
g_timer_stop (GTimer *timer);
Copy the code

Mark the end time, so calling g_timer_Elapsed () returns the difference between the end time and the start time.

g_timer_continue ()

void
g_timer_continue (GTimer *timer);
Copy the code

Restore the timer previously stopped with gtimer_stop (). You must call gtimer_stop () before using this function.

g_timer_elapsed ()

gdouble
g_timer_elapsed (GTimer *timer,
                 gulong *microseconds);
Copy the code

Gets the time since the timer started if it has not been stopped. Gets the interval between starting the timer and stopping the timer if the timer has stopped. The return value is the number of seconds elapsed, including any decimal part. The microsecond output parameter is basically useless.

g_timer_reset ()

void
g_timer_reset (GTimer *timer);
Copy the code

This function is useless; G_timer_start () can be called on timers that have already started to reset the start time, so g_timer_reset () is useless.

g_timer_destroy ()

void
g_timer_destroy (GTimer *timer);
Copy the code

Destroy the timer to free the related resources.

g_timer_is_active ()

gboolean
g_timer_is_active (GTimer *timer);
Copy the code

Determines whether the timer is currently active