2019-05-07 21:39:02 +02:00
# include <stdio.h>
2019-05-07 22:35:58 +02:00
# include <stdlib.h>
# include <pthread.h>
2019-05-07 21:39:02 +02:00
# include "routing4SITA.h"
2019-05-08 00:52:12 +02:00
pthread_mutex_t mutex ;
2019-05-07 22:35:58 +02:00
void * Search ( void * arguments ) ;
2019-05-07 21:39:02 +02:00
int main ( ) {
printf ( " hello world! \n " ) ;
2019-05-07 23:04:36 +02:00
2019-05-08 00:52:12 +02:00
pthread_mutex_init ( & mutex , NULL ) ;
2019-05-08 00:18:30 +02:00
2019-05-07 22:35:58 +02:00
pthread_t startThread ;
THREAD_DATA_t * data ;
data = ( THREAD_DATA_t * ) malloc ( sizeof ( THREAD_DATA_t ) ) ;
2019-05-07 23:04:36 +02:00
data - > node = getStartNode ( ) ;
2019-05-07 22:35:58 +02:00
data - > cost = 0 ;
2019-05-08 00:18:30 +02:00
data - > depth = 0 ;
data - > predecessors [ 0 ] = data - > node - > id ;
data - > bestpath = ( BESTPATH_t * ) malloc ( sizeof ( BESTPATH_t ) ) ;
data - > bestpath - > lowestCost = - 1 ;
data - > bestpath - > depth = 0 ;
2019-05-07 23:04:36 +02:00
2019-05-07 22:35:58 +02:00
pthread_create ( & startThread , NULL , Search , data ) ;
2019-05-08 00:18:30 +02:00
pthread_join ( startThread , NULL ) ;
2019-05-08 00:42:24 +02:00
printf ( " \n Best Path: %i " , data - > bestpath - > predecessorsBestPath [ 0 ] ) ;
2019-05-08 00:52:12 +02:00
for ( int i = 1 ; i < = data - > bestpath - > depth ; i + + ) {
printf ( " --> %i " , data - > bestpath - > predecessorsBestPath [ i ] ) ;
2019-05-08 00:18:30 +02:00
}
printf ( " \n With cost of: %i \n " , data - > bestpath - > lowestCost ) ;
2019-05-08 00:52:12 +02:00
2019-05-07 23:04:36 +02:00
for ( ; ; ) {
2019-05-07 22:35:58 +02:00
}
2019-05-07 23:04:36 +02:00
2019-05-07 22:35:58 +02:00
return 0 ;
}
2019-05-07 23:04:36 +02:00
void * Search ( void * arguments ) {
2019-05-07 22:35:58 +02:00
THREAD_DATA_t * data = ( THREAD_DATA_t * ) arguments ;
2019-05-08 00:52:12 +02:00
//printf("Number: %i\n", data->node->id);
//printf("Cost to this node: %i\n", data->cost);
2019-05-07 23:04:36 +02:00
if ( data - > node - > id = = 8 ) {
2019-05-08 00:52:12 +02:00
//printf("!!!End found! Cost: %i\n", data->cost);
2019-05-08 00:18:30 +02:00
//critical section start
2019-05-08 00:52:12 +02:00
pthread_mutex_lock ( & mutex ) ;
2019-05-08 00:18:30 +02:00
if ( data - > bestpath - > lowestCost = = - 1 ) {
//first solution found
2019-05-08 00:52:12 +02:00
// printf("first solution found! %i\n", data->depth);
2019-05-08 00:18:30 +02:00
data - > bestpath - > lowestCost = data - > cost ;
data - > bestpath - > depth = data - > depth ;
for ( int j = 0 ; j < data - > depth ; j + + ) {
data - > bestpath - > predecessorsBestPath [ j ] = data - > predecessors [ j ] ;
}
2019-05-08 00:52:12 +02:00
data - > bestpath - > predecessorsBestPath [ ( data - > bestpath - > depth ) ] = data - > node - > id ;
2019-05-08 00:18:30 +02:00
} else {
if ( data - > bestpath - > lowestCost > data - > cost ) {
//new best solution found
2019-05-08 00:52:12 +02:00
//printf("new best solution found! %i\n", data->depth);
2019-05-08 00:18:30 +02:00
data - > bestpath - > lowestCost = data - > cost ;
data - > bestpath - > depth = data - > depth ;
for ( int j = 0 ; j < data - > depth ; j + + ) {
data - > bestpath - > predecessorsBestPath [ j ] = data - > predecessors [ j ] ;
}
2019-05-08 00:52:12 +02:00
data - > bestpath - > predecessorsBestPath [ ( data - > bestpath - > depth ) ] = data - > node - > id ;
2019-05-08 00:18:30 +02:00
}
}
2019-05-08 00:52:12 +02:00
//critical section end
pthread_mutex_unlock ( & mutex ) ;
2019-05-07 22:35:58 +02:00
return NULL ;
}
2019-05-07 23:04:36 +02:00
if ( data - > node - > nodescount = = 0 ) {
2019-05-08 00:52:12 +02:00
// printf("No next nodes\n");
2019-05-07 22:35:58 +02:00
return NULL ;
}
2019-05-07 23:04:36 +02:00
2019-05-07 22:35:58 +02:00
pthread_t threads [ data - > node - > nodescount ] ;
for ( int i = 0 ; i < data - > node - > nodescount ; i + + ) {
2019-05-07 23:04:36 +02:00
THREAD_DATA_t * nextdata ;
nextdata = ( THREAD_DATA_t * ) malloc ( sizeof ( THREAD_DATA_t ) ) ;
nextdata - > node = ( data - > node - > nodesnext [ i ] ) ;
2019-05-08 00:18:30 +02:00
nextdata - > bestpath = data - > bestpath ;
2019-05-07 23:04:36 +02:00
nextdata - > cost = data - > node - > nodescost [ i ] + data - > cost ;
2019-05-08 00:42:24 +02:00
nextdata - > depth = data - > depth + 1 ;
2019-05-08 00:18:30 +02:00
for ( int j = 0 ; j < data - > depth ; j + + ) {
nextdata - > predecessors [ j ] = data - > predecessors [ j ] ;
}
2019-05-08 00:42:24 +02:00
nextdata - > predecessors [ ( ( nextdata - > depth ) - 1 ) ] = data - > node - > id ;
2019-05-07 23:04:36 +02:00
pthread_create ( & threads [ i ] , NULL , Search , nextdata ) ;
2019-05-07 22:35:58 +02:00
}
2019-05-08 00:52:12 +02:00
for ( int i = 0 ; i < data - > node - > nodescount ; i + + ) {
2019-05-08 00:18:30 +02:00
pthread_join ( threads [ i ] , NULL ) ;
2019-05-08 00:52:12 +02:00
}
2019-05-07 23:04:36 +02:00
2019-05-07 22:35:58 +02:00
return NULL ;
2019-05-07 21:39:02 +02:00
}
2019-05-07 22:35:58 +02:00