Skip to content

Instantly share code, notes, and snippets.

@benpicco
Created October 28, 2013 04:09
Show Gist options
  • Save benpicco/7191330 to your computer and use it in GitHub Desktop.
Save benpicco/7191330 to your computer and use it in GitHub Desktop.
Calculation gets interrupted, one timer is lost
#include <stdio.h>
#include "vtimer.h"
#include "thread.h"
#include "msg.h"
char timer_stack[KERNEL_CONF_STACKSIZE_DEFAULT];
struct timer_msg {
vtimer_t timer;
timex_t interval;
char* msg;
};
struct timer_msg msg_a = { .timer = {0}, .interval = { .seconds = 1, .microseconds = 0}, .msg = "Hello World" };
struct timer_msg msg_b = { .timer = {0}, .interval = { .seconds = 1, .microseconds = 0}, .msg = "This is a Test" };
void timer_thread(void) {
printf("This is thread %d\n", thread_getpid());
while (1) {
msg_t m;
msg_receive(&m);
struct timer_msg* tmsg = (struct timer_msg*) m.content.ptr;
printf("every %u.%us: %s\n", tmsg->interval.seconds, tmsg->interval.microseconds, tmsg->msg);
if (vtimer_set_msg(&tmsg->timer, tmsg->interval, thread_getpid(), tmsg) != 0)
puts("something went wrong");
double result = 0;
for (int i = 1; i < 10000; ++i)
for (int j = 1; j < 10000; ++j)
result += i/j;
printf("%.2f\n", result);
}
}
int main(void) {
msg_t m;
int pid = thread_create(timer_stack, sizeof timer_stack, PRIORITY_MAIN-1, CREATE_STACKTEST, timer_thread, "timer");
puts("sending 1st msg");
m.content.ptr = (char*) &msg_a;
msg_send(&m, pid, false);
puts("sending 2nd msg");
m.content.ptr = (char*) &msg_b;
msg_send(&m, pid, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment