Skip to content

Instantly share code, notes, and snippets.

@nattoheaven
Created June 25, 2013 22:55
Show Gist options
  • Save nattoheaven/5863199 to your computer and use it in GitHub Desktop.
Save nattoheaven/5863199 to your computer and use it in GitHub Desktop.
Test of Transactional Memory
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static double zr = 0.0;
static double zi = 0.0;
static const double cr = 0.1;
static const double ci = 0.1;
#ifndef TSX
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static void
*run(void *arg)
{
for (int i = 0; i < 1000000; ++i) {
#ifdef TSX
__transaction_atomic
{
#else
pthread_mutex_lock(&mutex);
#endif
double newzr = zr * zr - zi * zi + cr;
double newzi = 2 * zr * zi + ci;
zr = newzr;
zi = newzi;
#ifdef TSX
}
#else
pthread_mutex_unlock(&mutex);
#endif
}
return 0;
}
int
main(int argc, char **argv)
{
int n = atoi(argv[1]);
pthread_t *threads = new pthread_t[n];
for (int i = 0; i < n; ++i) {
pthread_create(&threads[i], 0, run, 0);
}
for (int i = 0; i < n; ++i) {
pthread_join(threads[i], 0);
}
printf("%f + %fi\n", zr, zi);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment