Done in one go xP
This commit is contained in:
parent
f6a18f46da
commit
9aa7973d2f
3 changed files with 135 additions and 0 deletions
114
threads/main.c
Normal file
114
threads/main.c
Normal file
|
@ -0,0 +1,114 @@
|
|||
#include "main.h"
|
||||
|
||||
|
||||
#define SIZE 1
|
||||
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_cond_t myCond1, myCond2;
|
||||
int myCount;
|
||||
|
||||
int isProduced = 0;
|
||||
|
||||
int main() {
|
||||
|
||||
printf("PROGRAM START\n");
|
||||
fflush(stdout);
|
||||
|
||||
// Create the consumer thread
|
||||
pthread_t consumerID;
|
||||
int resultInt = pthread_create(&consumerID, NULL, consumer, NULL);
|
||||
|
||||
producer();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void producer() {
|
||||
|
||||
while (1) {
|
||||
|
||||
pthread_mutex_lock(&myMutex);
|
||||
print_mutex("PRODUCER", "locked");
|
||||
|
||||
|
||||
while (isProduced == SIZE) {
|
||||
print_wait("PRODUCER", "myCond2");
|
||||
pthread_cond_wait(&myCond2, &myMutex);
|
||||
}
|
||||
|
||||
if(myCount == 10)
|
||||
end_program();
|
||||
|
||||
myCount++;
|
||||
isProduced++;
|
||||
|
||||
pthread_mutex_unlock(&myMutex);
|
||||
print_mutex("PRODUCER", "unlocked");
|
||||
pthread_cond_signal(&myCond1);
|
||||
print_signal("PRODUCER", "myCond1");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void* consumer() {
|
||||
|
||||
printf("CONSUMER THREAD CREATED\n");
|
||||
fflush(stdout);
|
||||
|
||||
while (1) {
|
||||
|
||||
pthread_mutex_lock(&myMutex);
|
||||
print_mutex("CONSUMER", "locked");
|
||||
|
||||
while (isProduced == 0) {
|
||||
print_wait("CONSUMER", "myCond1");
|
||||
pthread_cond_wait(&myCond1, &myMutex);
|
||||
}
|
||||
|
||||
//get_item();
|
||||
print_count();
|
||||
isProduced--;
|
||||
|
||||
pthread_mutex_unlock(&myMutex);
|
||||
print_mutex("CONSUMER", "unlocked");
|
||||
pthread_cond_signal(&myCond2);
|
||||
print_signal("CONSUMER", "myCond1");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void end_program() {
|
||||
|
||||
printf("PROGRAM END\n");
|
||||
fflush(stdout);
|
||||
exit(0);
|
||||
|
||||
}
|
||||
void print_mutex(char* type, char* lockstate) {
|
||||
|
||||
printf("%s: myMutex %s\n", type, lockstate);
|
||||
fflush(stdout);
|
||||
|
||||
}
|
||||
|
||||
void print_count() {
|
||||
|
||||
printf("myCount: %d -> %d\n", myCount-1, myCount);
|
||||
fflush(stdout);
|
||||
|
||||
}
|
||||
|
||||
void print_wait(char* type, char* var) {
|
||||
|
||||
printf("%s: waiting on %s\n", type, var);
|
||||
fflush(stdout);
|
||||
|
||||
}
|
||||
|
||||
void print_signal(char* type, char* var) {
|
||||
|
||||
printf("%s: signaling %s\n", type, var);
|
||||
fflush(stdout);
|
||||
|
||||
}
|
13
threads/main.h
Normal file
13
threads/main.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "pthread.h"
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
|
||||
void* consumer();
|
||||
void producer();
|
||||
void end_program();
|
||||
|
||||
void print_mutex(char*, char*);
|
||||
void print_count();
|
||||
void print_signal(char*, char*);
|
||||
void print_wait(char*, char*);
|
8
threads/makefile
Normal file
8
threads/makefile
Normal file
|
@ -0,0 +1,8 @@
|
|||
CC=gcc --std=gnu99 -g
|
||||
output=myCounter
|
||||
|
||||
all: main.c main.h
|
||||
$(CC) main.c -o $(output)
|
||||
|
||||
clean:
|
||||
rm -fr *.o vgcore.* $(output)
|
Loading…
Reference in a new issue