Last active
September 28, 2015 23:24
-
-
Save gallir/5df222892b1f633c8275 to your computer and use it in GitHub Desktop.
GCC __transaction_atomic bomb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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