Hi, that's my first bigger work in C++. Programm works good but I don't know where I have to alocate memory. And for which structures or something else I have to do alocation? Can anybody help me? Thx.
There is my code:
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <pthread.h>
#include <queue>
#include <map>
using namespace std;
useconds_t WAITING_TIME_UNIT = 100000;
map<pthread_t, int> userMap;
map<pthread_t, int>::iterator it;
pair<map<pthread_t, int>::iterator, bool> ret;
queue<pthread_t> queueA[5][2];
pthread_t threadPerson, threadElevator;
pthread_mutex_t mutexMove, mutexMove2, mutexCout;
pthread_cond_t condMove, condMove2;
struct Person {
int id, fromLevel, toLevel, direction; // direction: 1 = up, 0 = down
};
struct Elevator {
int actualLevel, numberOfPeople, direction;
};
Elevator elevator;
void* doElevator(void *null) {
// there is code for elevator thread
// there is used variable: elevator
}
void* doPerson(void *person) {
Person man;
man = *((Clovek*) person);
// there is code for people thread
// there is used variable: man, elevator
}
int createPerson(string s) {
string idS, fromLevelS, toLevelS;
int check = 1, id, fromLevel, toLevel;
Person user;
// I check input if it's all right
user.id = id;
user.fromLevel = fromLevel;
user.toLevel = toLevel;
if (fromLevel < toLevel) {
user.direction = 1; // up
} else if (fromLevel > toLevel) {
user.direction = 0; // down
} else { // fromLevel == toLevel ... error
return 0;
}
int ok;
ok = pthread_create(&threadPerson, NULL, doPerson, (void *) & user);
if (ok != 0) {
return 0; // thread not made
}
return 1;
}
int main(int argc, char** argv) {
string input;
elevator.actualLevel = 1; // default set up of elevator
elevator.numberOfPeople = 0; // default set up of elevator
elevator.direction = 1; // default set up of elevator
pthread_mutex_init(&mutexMove, NULL);
pthread_mutex_init(&mutexMove2, NULL);
pthread_mutex_init(&mutexCout, NULL);
pthread_cond_init(&condMove, NULL);
pthread_cond_init(&condMove2, NULL);
if (pthread_create(&threadElevator, NULL, doElevator, NULL) != 0) {
destroy(); // destroys mutexes and conditions variables
exit(EXIT_FAILURE);
}
while (!feof(stdin)) {
cin >> input;
if(createPerson(input) == 0) {
destroy(); // destroys mutexes and conditions variables
exit(EXIT_FAILURE);
}
}
pthread_join(threadElevator, NULL); // wait on threadElevator exit
if(isAllEmpty() == 1) { // check if all queues and map are empty
destroy(); // destroys mutexes and conditions variables
return(EXIT_SUCCESS);
} else {
destroy(); // destroys mutexes and conditions variables
return(EXIT_FAILURE);
}
}