Skip to content

Instantly share code, notes, and snippets.

@vani2
Created April 29, 2015 13:15
Show Gist options
  • Save vani2/37d5eaea247b8723e6cc to your computer and use it in GitHub Desktop.
Save vani2/37d5eaea247b8723e6cc to your computer and use it in GitHub Desktop.
Just sample of multi-thread in C
typedef struct {
char *str;
int size;
int counter[256];
} thread_args;
void* threadFunc(void *args) {
thread_args *params = (thread_args *) args;
for (int i = params->size/2; i < params->size; i++) {
params->counter[params->str[i]]++;
}
return NULL;
}
char mostFrequentCharacter(char* str, int size) {
thread_args args = {.str = str, .size = size, .counter = {0}};
char result = NULL;
int occurences = 0;
pthread_t thread;
pthread_create(&thread, NULL, threadFunc, &args);
for (int i = 0; i < args.size/2; i++) {
args.counter[args.str[i]]++;
}
pthread_join(thread, NULL);
for (int i = 0; i < 256; i++) {
if (args.counter[i] > occurences) {
occurences = args.counter[i];
result = (char) i;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment