Skip to content

Instantly share code, notes, and snippets.

@alex-eg
Last active June 16, 2016 17:00
Show Gist options
  • Select an option

  • Save alex-eg/1d08f774ba6b97b92849ba742f003eaf to your computer and use it in GitHub Desktop.

Select an option

Save alex-eg/1d08f774ba6b97b92849ba742f003eaf to your computer and use it in GitHub Desktop.
// cc -o fun pthreads-megafun.c -g -std=c99 -lpthread
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
static void *start(void *arg) {
int code = (int)arg;
if (0 == code) {
printf("I won't crash!\n");
sleep(10);
} else {
printf("Crahsing n_n...\n");
sleep(5);
*(int*)arg = 34; // YOLO
}
}
int main(int argc, char **argv) {
int flag = 1;
if (argc > 1) {
if (strcmp(argv[1], "-nofail")) {
printf("Usage:\n -nofail -- don't fail\n");
return 0;
} else {
flag = 0;
}
}
srand(time(NULL));
pthread_t threads[10];
for (int i = 0; i < 10; i++) {
int code = 0;
if (flag) {
if (rand() % 3 == 0) {
printf("Thread %d will crash!\n", i);
code = 1;
flag = 0;
}
}
pthread_create(&threads[i], NULL, &start, (void*)code);
}
pthread_join(threads[4], NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment