Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unistd.h>
- #include <pthread.h>
- #include <time.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdbool.h>
- #include <sys/stat.h>
- #include <sys/file.h>
- #include <semaphore.h>
- #include <sys/mman.h>
- //using namespace std;
- struct thread_arg{
- std::vector<int>freelist;
- std::vector<int>list1;
- std::vector<int>list2;
- };
- sem_t mutex1, mutex2, mutex3, freemax, freecnt, cnt1, cnt2;
- struct thread_arg Args;
- void *thread1_function(void *Args){
- struct thread_arg *stuff = (struct thread_arg*)Args;
- int node;
- while (1){
- std::cout << "Thread ONE started\n";
- // Lock our mutexes freemax, freecnt, mutex1
- sem_wait(&freecnt);
- sem_wait(&mutex1);
- // Unlink
- node = stuff->freelist.back();
- stuff->freelist.pop_back();
- // Unlock mutex 1
- sem_post(&mutex1);
- // Do stuff/produce info/ use 'node' value
- std::cout << "Thread1 produce info after unlinking" << "\n";
- // Lock mutex2 after producing info
- sem_wait(&mutex2);
- // Link
- stuff->list1.push_back(node);
- std::cout << "lock mutex2 and link list1\n";
- // Free mutex2 and cnt1
- sem_post(&mutex2);
- sem_post(&cnt1);
- }
- return NULL;
- }
- void *thread2_function(void *Args){
- struct thread_arg *stuff = (struct thread_arg*)Args;
- int x,y;
- while (1){
- std::cout << "Thread TWO started\n";
- // Lock our cnt1, mutex2
- sem_wait(&cnt1);
- sem_wait(&mutex2);
- // Unlink list1
- x = stuff->list1.back();
- stuff->list1.pop_back();
- // Unlock mutex2, lock freecnt
- sem_post(&mutex2);
- sem_wait(&freecnt);
- // Lock mutex 1
- sem_wait(&mutex1);
- // Unlink freelist
- y = stuff->freelist.back();
- stuff->freelist.pop_back();
- // Unlock mutex1, freemax
- sem_post(&mutex1);
- sem_post(&freemax);
- // Produce info/use block
- y= x + y;
- // Lock mutex1
- sem_wait(&mutex1);
- // Link x, freelist
- stuff->freelist.push_back(x);
- // Unlock mutex1, freecnt
- sem_post(&mutex1);
- sem_post(&freecnt);
- // Lock mutex3
- sem_wait(&mutex3);
- // Link y, list2
- stuff->list2.push_back(y);
- // Unlock mutex3, cnt2
- sem_post(&mutex3);
- sem_post(&cnt2);
- }
- return NULL;
- }
- void *thread3_function(void *Args){
- struct thread_arg *stuff = (struct thread_arg*)Args;
- int node;
- while(1){
- std::cout << "Thread THREE started\n";
- // Lock cnt2, mutex3
- sem_wait(&cnt2);
- sem_wait(&mutex3);
- // Unlink
- node = stuff->list2.back();
- stuff->list2.pop_back();
- // Unlock mutex3
- sem_post(&mutex3);
- //consume info in block with node
- std::cout << "Consuming info in block with var node\n";
- // Lock mutex1
- sem_wait(&mutex1);
- // Link freelist
- stuff->freelist.push_back(node);
- // Unlock mutex1, freecnt
- sem_post(&mutex1);
- sem_post(&freecnt);
- }
- return NULL;
- }
- int main(){
- sem_init(&mutex1, 1, 1);
- sem_init(&mutex2, 1, 1);
- sem_init(&mutex3, 1, 1);
- Args.freelist.assign(10,0);
- sem_init(&freecnt, 1, Args.freelist.size());
- sem_init(&freemax, 1, Args.freelist.size());
- sem_init(&cnt1, 1, 0);
- sem_init(&cnt2, 1, 0);
- pthread_t thread1,thread2,thread3;
- int t1,t2,t3;
- pthread_create(&thread1, NULL, thread1_function, &Args);
- pthread_create(&thread2, NULL, thread2_function, &Args);
- pthread_create(&thread3, NULL, thread3_function, &Args);
- pthread_join(thread1,NULL);
- pthread_join(thread2,NULL);
- pthread_join(thread3,NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement