Commit 6faa235c authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added pthread example code

parent 02002927
# To run: make -f Makefile.threads
#
# For compiling on PC
CC=gcc
PRGS=thread_example thread_example_monitor thread_example_semaphore
.PHONY: all clean distclean
all: $(PRGS)
# CPPFLAGS+=-DDEBUG
# CPPFLAGS+=-DSHORT_NUMBER_FRAME_SEQUENCE
CFLAGS+=-g -Wall -Werror -pedantic -pedantic-errors
LDLIBS=-lpthread
%: %.c
clean:
distclean: clean
-rm $(PRGS)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cnd = PTHREAD_COND_INITIALIZER;
struct global_data{
const int count;
};
void * task_a(void * ctx)
{
struct global_data *d = ctx;
int i;
printf("started task_a\n");
for(i=0; i < d->count; ++i){
printf("task_a: %d\n",i);
}
return NULL;
}
void * task_b(void * ctx)
{
struct global_data *d = ctx;
int i;
printf("started task_b\n");
for(i=0; i < d->count; ++i){
printf("task_b: %d\n",i);
}
return NULL;
}
int main()
{
struct global_data data = {10};
pthread_t thread_a;
pthread_t thread_b;
if(pthread_create(&thread_a, NULL, task_a, &data)){
printf("Failed to create thread_a\n");
exit(1);
}
if(pthread_create(&thread_b, NULL, task_b, &data)){
printf("Failed to create thread_b\n");
exit(2);
}
pthread_join(thread_a, NULL);
pthread_join(thread_b, NULL);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cnd = PTHREAD_COND_INITIALIZER;
struct global_data{
char task;
const int count;
};
void * task_a(void * ctx)
{
struct global_data *d = ctx;
int i;
printf("started task_a\n");
for(i=0; i < d->count; ++i){
pthread_mutex_lock(&mtx);
while(d->task != 'a') pthread_cond_wait(&cnd, &mtx);
printf("task_a: %d\n",i);
d->task = 'b';
pthread_cond_signal(&cnd);
pthread_mutex_unlock(&mtx);
}
return NULL;
}
void * task_b(void * ctx)
{
struct global_data *d = ctx;
int i;
printf("started task_b\n");
for(i=0; i < d->count; ++i){
pthread_mutex_lock(&mtx);
while(d->task != 'b') pthread_cond_wait(&cnd, &mtx);
printf("task_b: %d\n",i);
d->task = 'a';
pthread_cond_signal(&cnd);
pthread_mutex_unlock(&mtx);
}
return NULL;
}
int main()
{
struct global_data data = {0,10};
pthread_t thread_a;
pthread_t thread_b;
if(pthread_create(&thread_a, NULL, task_a, &data)){
printf("Failed to create thread_a\n");
exit(1);
}
if(pthread_create(&thread_b, NULL, task_b, &data)){
printf("Failed to create thread_b\n");
exit(2);
}
sleep(1);
pthread_mutex_lock(&mtx);
printf("setting task to a\n");
data.task = 'a';
pthread_cond_signal(&cnd);
pthread_mutex_unlock(&mtx);
pthread_join(thread_a, NULL);
pthread_join(thread_b, NULL);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
struct global_data{
int count;
sem_t sem_a;
sem_t sem_b;
};
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
void * task_a(void * ctx)
{
struct global_data *d = ctx;
int i;
printf("started task_a\n");
for(i=0; i < d->count; ++i){
sem_wait(&d->sem_a);
pthread_mutex_lock(&mtx);
printf("task_a: %d\n",i);
sem_post(&d->sem_b);
pthread_mutex_unlock(&mtx);
}
return NULL;
}
void * task_b(void * ctx)
{
struct global_data *d = ctx;
int i;
printf("started task_b\n");
for(i=0; i < d->count; ++i){
sem_wait(&d->sem_b);
pthread_mutex_lock(&mtx);
printf("task_b: %d\n",i);
sem_post(&d->sem_a);
pthread_mutex_unlock(&mtx);
}
return NULL;
}
int global_data_init(struct global_data *d)
{
d->count = 10;
if(sem_init(&d->sem_a, 0, 0)){
return 1;
}
if(sem_init(&d->sem_b, 0, 0)){
return 1;
}
return 0;
}
int main()
{
struct global_data data;
pthread_t thread_a;
pthread_t thread_b;
int res;
if(global_data_init(&data)){
perror("global_data_init");
exit(1);
}
if((res=pthread_create(&thread_a, NULL, task_a, &data))){
errno = res;
perror("Failed to create thread_a\n");
exit(2);
}
if((res=pthread_create(&thread_b, NULL, task_b, &data))){
errno = res;
perror("Failed to create thread_a\n");
exit(3);
}
sleep(1);
pthread_mutex_lock(&mtx);
printf("signalling semaphore\n");
sem_post(&data.sem_a);
pthread_mutex_unlock(&mtx);
pthread_join(thread_a, NULL);
pthread_join(thread_b, NULL);
return 0;
}
Supports Markdown
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