Last active
August 29, 2015 14:05
-
-
Save tengomucho/28db406b487d63b67e40 to your computer and use it in GitHub Desktop.
A sample to use to test profilers (for example oprofile). To be compiled like this: gcc -o testoprof -lpthread testoprof.c
This file contains 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
/* 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