9 volatile int sensor_value;
12 void Handler_Sensors(void *arg, char *params)
14 const char *key, *value;
16 Data *data = (Data*) arg;
18 //Begin a request only when you know the final result
19 //E.g whether OK or not.
20 FCGI_BeginJSON(STATUS_OK, "sensors");
21 while ((params = FCGI_KeyPair(params, &key, &value))) {
22 FCGI_BuildJSON(key, value);
24 pthread_mutex_lock(&(data->mutex));
25 snprintf(buf, 128, "%d", data->sensor_value);
26 FCGI_BuildJSON("sensor_value", buf);
27 pthread_mutex_unlock(&(data->mutex));
31 void *SensorsLoop(void *arg) {
32 Data *data = (Data*) arg;
36 pthread_mutex_lock(&(data->mutex));
37 data->sensor_value = rand() % 1024;
38 pthread_mutex_unlock(&(data->mutex));
39 usleep(200*1000); //200ms
43 int main(int argc, char *argv[]) {
45 pthread_mutex_init(&(data.mutex), NULL);
46 //data.mutex = PTHREAD_MUTEX_INITIALIZER;
48 // pthread_create(&data.sensors, NULL, SensorsLoop, &data);
49 FCGI_RequestLoop(&data);