Programming/C,C++

Callback 예제 코드

루나s 2017. 1. 19. 18:10

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <Windows.h>


#define random(n) (rand() % (n))


typedef int(*Dm_callback)(void *, unsigned int, unsigned int);


typedef struct Dm {

void *context;

Dm_callback callbak;

} Dm;


int DmCallback(void *Dm, unsigned int time, unsigned int time2)

{

printf("callback %u - %u= %d\n", time2, time, time2 - time);

}


int main(void)

{

unsigned int time, time2;

unsigned int diff;

Dm *dm;


dm = malloc(sizeof(dm));

dm->context = dm;

dm->callbak = DmCallback;

time = GetTickCount();


while (1) {

Sleep(random (100));

time2 = GetTickCount();

if (time2 - time > 1000) {

dm->callbak(dm->context, time, time2);

time = time2;

}

}

return 0;

}