Skip to content

Instantly share code, notes, and snippets.

View matutter's full-sized avatar

Mat Utter matutter

View GitHub Profile
@matutter
matutter / gist:9956647
Created April 3, 2014 15:32
nested for loops and running average
for ( y = 0; y < rows ; y++)
{
for ( x = 0; x < cols ; x++)
{
number[y][x] = (rand() % 10001 + 5);
if(hi < number[y][x]) hi= number[y][x];
count++;
avg-=avg/(float)count;
avg+=number[y][x]/(float)count;
}
@matutter
matutter / file_event_iNotify.h
Created March 12, 2014 20:28
Detecting file modifications the efficient way using the inotify library
#include <sys/inotify.h> //filesystem events
class tinyListener {
public:
string name, list_name;
void addListener(string s) {
list_name = s;
}
bool good() {
return (list_name == name);
}
@matutter
matutter / thread_race.cpp
Created March 12, 2014 02:55
An example demonstrating c++ threads and how mutex's work to ensure critical operation order.
// the compile args -- gcc version 4.6.3 //
// g++ this.cpp -Wall -fexceptions -std=c++0x -g -lpthread //
//////////////////////////////////////////////////////////////
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mx;
@matutter
matutter / file_change_event.h
Created February 28, 2014 05:25
The basics of detecting a file change event without excess libraries. Very inefficient.
#include <cstring>
#define _missingFile 0x601DE0
using namespace std;
class fileListener
{
private:
struct stat st;
public:
class file_
@matutter
matutter / basic_parent_child_wait.c
Last active August 29, 2015 13:56
using wait() to prevent children from zombiefying
/* Using 'wait();' and an '__EXIT_STATUS' makes sure we don't have zombies or a race condition */
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
void child(int*);
int parent(int);
int main(void)
{
pid_t pid=1;
@matutter
matutter / basic_parent_child_fork.c
Last active August 29, 2015 13:56
a basic example of parent/child that creates zombies and a race condition
#include <stdio.h>
#include <sys/types.h>
void child(int*);
int parent(int);
void main(void)
{
pid_t pid;
int n=0, i=0;
do {
if (pid == 0)