Skip to content

Instantly share code, notes, and snippets.

@dirac3000

dirac3000/testoprof.c

Last active Aug 29, 2015
Embed
What would you like to do?
A sample to use to test profilers (for example oprofile). To be compiled like this: gcc -o testoprof -lpthread testoprof.c
/* I know this is no good, but I want to use strfry */
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define SIZE 500000
#define NTHREADS 4
static pthread_t *thread_ids;
static pthread_mutex_t thread_lock;
static int* ids;
static int compare(const void *s1, const void *s2) { return strcmp(s1, s2); }
void* action(void *ptr) {
int i;
char *strings[SIZE];
char str[] = "abcdefghijklmnopqrstuvwxyz";
int id = *(int*)ptr;
pthread_mutex_lock(&thread_lock);
printf("thread %d started\n", id);
pthread_mutex_unlock(&thread_lock);
for (i = 0; i < SIZE; ++i) {
strings[i] = strdup(str);
strfry(strings[i]);
}
pthread_mutex_lock(&thread_lock);
printf("thread %d init done\n", id);
pthread_mutex_unlock(&thread_lock);
qsort(strings, SIZE, sizeof(char *), compare);
pthread_mutex_lock(&thread_lock);
printf("thread %d over\n", id);
pthread_mutex_unlock(&thread_lock);
return NULL;
}
int main(int argc, char** argv) {
int i;
pthread_mutex_init(&thread_lock, NULL);
thread_ids = malloc(sizeof(pthread_t) * NTHREADS);
ids = malloc(sizeof(int) * NTHREADS);
for (i = 0; i < NTHREADS; i++) {
ids[i] = i;
pthread_create(&thread_ids[i], NULL, action, &ids[i]);
}
/* wait for the the threads to finish */
for (i = 0; i < NTHREADS; i++) {
/* pthread_cancel(thread_ids[i]); */
pthread_join(thread_ids[i], NULL);
}
// release the mutex resources
pthread_mutex_destroy(&thread_lock);
printf("All threads over.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.