114 lines
2.3 KiB
C
114 lines
2.3 KiB
C
#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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|