Skip to content

Instantly share code, notes, and snippets.

@gallir
Last active September 28, 2015 23:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gallir/5df222892b1f633c8275 to your computer and use it in GitHub Desktop.
Save gallir/5df222892b1f633c8275 to your computer and use it in GitHub Desktop.
GCC __transaction_atomic bomb
/* It will eat your memory
* But if you insist, compile it with:
gcc -pthread -fgnu-tm -mcx16 -o transaction_bomb transaction_bomb.c
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define ARRAY_SIZE 2
#define NUM_THREADS 2
#define MAX_COUNT 100000000
// Just used to send the index of the id
struct tdata {
int tid;
};
int counter[ARRAY_SIZE];
void *count(void *ptr) {
long i, max = MAX_COUNT/NUM_THREADS;
int tid = ((struct tdata *) ptr)->tid;
__transaction_atomic {
while (!counter[tid]);
counter[(tid + 1) % NUM_THREADS] = 1;
}
return;
}
int main (int argc, char *argv[]) {
pthread_t threads[NUM_THREADS];
int rc, i;
struct tdata id[NUM_THREADS];
for(i=0; i<NUM_THREADS; i++){
id[i].tid = i;
rc = pthread_create(&threads[i], NULL, count, (void *) &id[i]);
}
for(i=0; i<NUM_THREADS; i++){
pthread_join(threads[i], NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment