added mutex and cleanup
This commit is contained in:
parent
e336a1d4fc
commit
c144c43593
|
@ -1,6 +1,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "routing4SITA.h"
|
#include "routing4SITA.h"
|
||||||
|
|
||||||
|
|
||||||
NODE_t* getStartNode() {
|
NODE_t* getStartNode() {
|
||||||
|
|
||||||
static NODE_t* start;
|
static NODE_t* start;
|
||||||
|
|
BIN
src/routing4SITA
BIN
src/routing4SITA
Binary file not shown.
|
@ -4,14 +4,14 @@
|
||||||
|
|
||||||
#include "routing4SITA.h"
|
#include "routing4SITA.h"
|
||||||
|
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
|
||||||
void *Search(void* arguments);
|
void *Search(void* arguments);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
printf("hello world!\n");
|
printf("hello world!\n");
|
||||||
|
|
||||||
|
pthread_mutex_init (&mutex, NULL);
|
||||||
|
|
||||||
|
|
||||||
pthread_t startThread;
|
pthread_t startThread;
|
||||||
THREAD_DATA_t* data;
|
THREAD_DATA_t* data;
|
||||||
|
@ -25,15 +25,9 @@ int main() {
|
||||||
data->bestpath->depth = 0;
|
data->bestpath->depth = 0;
|
||||||
|
|
||||||
pthread_create(&startThread, NULL, Search, data);
|
pthread_create(&startThread, NULL, Search, data);
|
||||||
|
|
||||||
pthread_join(startThread, NULL);
|
pthread_join(startThread, NULL);
|
||||||
|
|
||||||
|
|
||||||
printf("pathdepth: %i\n", data->bestpath->depth);
|
|
||||||
|
|
||||||
|
|
||||||
printf("\nBest Path: %i", data->bestpath->predecessorsBestPath[0]);
|
printf("\nBest Path: %i", data->bestpath->predecessorsBestPath[0]);
|
||||||
|
|
||||||
for(int i = 1; i <= data->bestpath->depth; i++) {
|
for(int i = 1; i <= data->bestpath->depth; i++) {
|
||||||
printf(" --> %i", data->bestpath->predecessorsBestPath[i]);
|
printf(" --> %i", data->bestpath->predecessorsBestPath[i]);
|
||||||
}
|
}
|
||||||
|
@ -51,17 +45,18 @@ void *Search(void* arguments) {
|
||||||
|
|
||||||
THREAD_DATA_t *data = (THREAD_DATA_t*) arguments;
|
THREAD_DATA_t *data = (THREAD_DATA_t*) arguments;
|
||||||
|
|
||||||
printf("Number: %i\n", data->node->id);
|
//printf("Number: %i\n", data->node->id);
|
||||||
printf("Cost to this node: %i\n", data->cost);
|
//printf("Cost to this node: %i\n", data->cost);
|
||||||
|
|
||||||
if(data->node->id == 8) {
|
if(data->node->id == 8) {
|
||||||
printf("!!!End found! Cost: %i\n", data->cost);
|
//printf("!!!End found! Cost: %i\n", data->cost);
|
||||||
|
|
||||||
//critical section start
|
//critical section start
|
||||||
|
pthread_mutex_lock (&mutex);
|
||||||
|
|
||||||
if(data->bestpath->lowestCost == -1) {
|
if(data->bestpath->lowestCost == -1) {
|
||||||
//first solution found
|
//first solution found
|
||||||
printf("first solution found! %i\n", data->depth);
|
// printf("first solution found! %i\n", data->depth);
|
||||||
data->bestpath->lowestCost = data->cost;
|
data->bestpath->lowestCost = data->cost;
|
||||||
data->bestpath->depth = data->depth;
|
data->bestpath->depth = data->depth;
|
||||||
for(int j = 0; j < data->depth; j++) {
|
for(int j = 0; j < data->depth; j++) {
|
||||||
|
@ -72,7 +67,7 @@ void *Search(void* arguments) {
|
||||||
|
|
||||||
if(data->bestpath->lowestCost > data->cost) {
|
if(data->bestpath->lowestCost > data->cost) {
|
||||||
//new best solution found
|
//new best solution found
|
||||||
printf("new best solution found! %i\n", data->depth);
|
//printf("new best solution found! %i\n", data->depth);
|
||||||
data->bestpath->lowestCost = data->cost;
|
data->bestpath->lowestCost = data->cost;
|
||||||
data->bestpath->depth = data->depth;
|
data->bestpath->depth = data->depth;
|
||||||
for(int j = 0; j < data->depth; j++) {
|
for(int j = 0; j < data->depth; j++) {
|
||||||
|
@ -82,14 +77,13 @@ void *Search(void* arguments) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//critical section end
|
//critical section end
|
||||||
|
pthread_mutex_unlock (&mutex);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(data->node->nodescount == 0) {
|
if(data->node->nodescount == 0) {
|
||||||
printf("No next nodes\n");
|
// printf("No next nodes\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,13 +99,8 @@ void *Search(void* arguments) {
|
||||||
|
|
||||||
for(int j = 0; j < data->depth; j++) {
|
for(int j = 0; j < data->depth; j++) {
|
||||||
nextdata->predecessors[j] = data->predecessors[j];
|
nextdata->predecessors[j] = data->predecessors[j];
|
||||||
|
|
||||||
printf("data->predecessors: %i\n", data->predecessors[j]);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nextdata->predecessors[((nextdata->depth) - 1)] = data->node->id;
|
nextdata->predecessors[((nextdata->depth) - 1)] = data->node->id;
|
||||||
|
|
||||||
pthread_create(&threads[i], NULL, Search, nextdata);
|
pthread_create(&threads[i], NULL, Search, nextdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,113 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include "routing4SITA.h"
|
|
||||||
|
|
||||||
|
|
||||||
void *Search(void* arguments);
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
printf("hello world!\n");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pthread_t startThread;
|
|
||||||
THREAD_DATA_t* data;
|
|
||||||
data = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t));
|
|
||||||
data->node = getStartNode();
|
|
||||||
data->cost = 0;
|
|
||||||
data->depth = 0;
|
|
||||||
data->predecessors[0] = data->node->id;
|
|
||||||
data->bestpath = (BESTPATH_t*) malloc(sizeof(BESTPATH_t));
|
|
||||||
data->bestpath->lowestCost = -1;
|
|
||||||
|
|
||||||
pthread_create(&startThread, NULL, Search, data);
|
|
||||||
|
|
||||||
for(;;) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void *Search(void* arguments) {
|
|
||||||
|
|
||||||
THREAD_DATA_t *data = (THREAD_DATA_t*) arguments;
|
|
||||||
|
|
||||||
printf("Number: %i\n", data->node->id);
|
|
||||||
printf("Cost to this node: %i\n", data->cost);
|
|
||||||
|
|
||||||
if(data->node->id == 8) {
|
|
||||||
printf("End found! Cost: %i\n", data->cost);
|
|
||||||
|
|
||||||
//critical section
|
|
||||||
|
|
||||||
if(data->bestpath->lowestCost == -1){
|
|
||||||
//first solution found
|
|
||||||
data->bestpath->lowestCost = data->cost;
|
|
||||||
for(int j = 0; j < data->depth; j++){
|
|
||||||
nextdata->predecessors[j] = data->predecessors[j];
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
|
|
||||||
if(data->bestpath->lowestCost > data->cost){
|
|
||||||
//new best solution found
|
|
||||||
data->bestpath->lowestCost = data->cost;
|
|
||||||
for(int j = 0; j < data->depth; j++){
|
|
||||||
nextdata->predecessors[j] = data->predecessors[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(data->node->nodescount == 0) {
|
|
||||||
printf("No next nodes\n");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_t threads[data->node->nodescount];
|
|
||||||
|
|
||||||
for(int i = 0; i < data->node->nodescount; i++) {
|
|
||||||
THREAD_DATA_t* nextdata;
|
|
||||||
nextdata = (THREAD_DATA_t*) malloc(sizeof(THREAD_DATA_t));
|
|
||||||
nextdata->node = (data->node->nodesnext[i]);
|
|
||||||
nextdata->cost = data->node->nodescost[i] + data->cost;
|
|
||||||
nextdata->depth++;
|
|
||||||
|
|
||||||
for(int j = 0; j < data->depth; j++){
|
|
||||||
nextdata->predecessors[j] = data->predecessors[j];
|
|
||||||
}
|
|
||||||
nextdata->predecessors[nextdata->depth] = data->node->id;
|
|
||||||
|
|
||||||
pthread_create(&threads[i], NULL, Search, nextdata);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue