Commit f57588cd authored by Tom Pointon's avatar Tom Pointon

Add benchmarks

parent 81cda78d
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int global_var = 0;
void thread_func(void);
void thread_func(void) {
pthread_mutex_unlock(&mutex);
global_var += 1;
pthread_mutex_lock(&mutex);
}
int main() {
pthread_t t1_id;
pthread_create(&t1_id, NULL, &thread_func, NULL);
}
\ No newline at end of file
#include <pthread.h>
pthread_cond_t cv;
int global_var = 0;
void thread_func_1(void);
void thread_func_1(void) {
global_var += 1;
}
void thread_func_2(void);
void thread_func_2(void) {
global_var += 2;
}
int main() {
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &thread_func_1, NULL);
pthread_create(&t2_id, NULL, &thread_func_2, NULL);
}
\ No newline at end of file
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int SUM = 0;
// Increments SUM in a thread-safe manner
void safe_increment_sum(void);
void safe_increment_sum(void) {
pthread_mutex_lock(&lock);
SUM += 1;
pthread_mutex_unlock(&lock);
}
int main(void) {
// Launch threads
pthread_t t1, t2;
pthread_create(&t1, NULL, &safe_increment_sum, NULL);
pthread_create(&t2, NULL, &safe_increment_sum, NULL);
pthread_join(&t1, NULL);
pthread_join(&t2, NULL);
printf("SUM=%d", SUM);
}
\ No newline at end of file
// Correctly synchronized buffer
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int buffer[256];
int main() {
memset(buffer, 0, sizeof(buffer));
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &write_to_buff, NULL);
pthread_create(&t1_id, NULL, &write_to_buff, NULL);
pthread_join(t1_id, NULL);
pthread_join(t2_id, NULL);
}
void *write_to_buff(void *vargp) {
pthread_mutex_lock(&mutex);
buffer[rand() % 256] = count += 1;
pthread_mutex_unlock(&mutex);
}
\ No newline at end of file
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int global_var = 0;
void thread_func(void);
void thread_func(void) {
pthread_mutex_lock(&mutex);
global_var += 1;
}
int main() {
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &thread_func, NULL);
pthread_create(&t2_id, NULL, &thread_func, NULL);
}
\ No newline at end of file
#include <pthread.h>
int global_var = 0;
void thread_func_1(void);
void thread_func_1(void) {
global_var += 1;
}
void thread_func_2(void);
void thread_func_2(void) {
global_var += 2;
}
int main() {
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &thread_func_1, NULL);
pthread_create(&t2_id, NULL, &thread_func_2, NULL);
}
\ No newline at end of file
#include <pthread.h>
#include <semaphore.h>
sem_t mutex;
int global_var = 0;
void thread_func_1(void);
void thread_func_1(void) {
global_var += 1;
sem_post(&mutex);
sem_post(&mutex);
}
void thread_func_2(void);
void thread_func_2(void) {
sem_wait(&mutex);
sem_wait(&mutex);
global_var += 1;
}
int main() {
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &thread_func_1, NULL);
pthread_create(&t2_id, NULL, &thread_func_2, NULL);
}
\ No newline at end of file
#include <pthread.h>
#include <semaphore.h>
sem_t mutex;
int global_var = 0;
void thread_func_1(void);
void thread_func_1(void) {
sem_post(&mutex);
sem_post(&mutex);
global_var += 1;
}
void thread_func_2(void);
void thread_func_2(void) {
sem_wait(&mutex);
sem_wait(&mutex);
sem_wait(&mutex);
sem_post(&mutex);
}
int main() {
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &thread_func_1, NULL);
pthread_create(&t2_id, NULL, &thread_func_2, NULL);
}
\ No newline at end of file
#include <pthread.h>
pthread_mutex_t lock_1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock_2 = PTHREAD_MUTEX_INITIALIZER;
int global_var = 0;
void thread_func(void);
void thread_func(void) {
pthread_mutex_lock(&lock_1);
pthread_mutex_lock(&lock_2);
global_var += 1;
pthread_mutex_unlock(&lock_2);
pthread_mutex_lock(&lock_1);
}
int main() {
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &thread_func, NULL);
pthread_create(&t2_id, NULL, &thread_func, NULL);
}
\ No newline at end of file
#include <pthread.h>
pthread_mutex_t lock_1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock_2 = PTHREAD_MUTEX_INITIALIZER;
int global_var = 0;
void thread_func(void);
void thread_func(void) {
pthread_mutex_lock(&lock_1);
pthread_mutex_lock(&lock_2);
global_var += 1;
pthread_mutex_unlock(&lock_1);
pthread_mutex_unlock(&lock_2);
}
int main() {
pthread_t t1_id;
pthread_t t2_id;
pthread_create(&t1_id, NULL, &thread_func, NULL);
pthread_create(&t2_id, NULL, &thread_func, NULL);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment