Soving knapsack problem

Dependencies:   mbed mbed-rtos

Committer:
vferman
Date:
Mon Mar 28 20:21:44 2022 +0000
Revision:
1:39e36a5c11e0
Parent:
0:1ea8993f6b18
Push

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vferman 0:1ea8993f6b18 1 #include "mbed.h"
vferman 0:1ea8993f6b18 2 #include "rtos.h"
vferman 1:39e36a5c11e0 3 #include <queue>
vferman 0:1ea8993f6b18 4
vferman 1:39e36a5c11e0 5
vferman 1:39e36a5c11e0 6 //#define DELAY_INSIDE
vferman 1:39e36a5c11e0 7 //#define DELAY_BTWN
vferman 1:39e36a5c11e0 8 #define DELAY_DISPLAY
vferman 1:39e36a5c11e0 9
vferman 1:39e36a5c11e0 10 #define DELAY_INSIDE_TIME 1000
vferman 1:39e36a5c11e0 11 #define DELAY_BTWN_TIME 311
vferman 1:39e36a5c11e0 12 #define DELAY_DISPLAY_TIME 100
vferman 1:39e36a5c11e0 13
vferman 0:1ea8993f6b18 14
vferman 1:39e36a5c11e0 15 #define ARRAY_SIZE 16
vferman 1:39e36a5c11e0 16 #define THREAD_ARRAY_SIZE 10
vferman 1:39e36a5c11e0 17
vferman 1:39e36a5c11e0 18 typedef struct{
vferman 1:39e36a5c11e0 19 int tid;
vferman 1:39e36a5c11e0 20 int pid;
vferman 1:39e36a5c11e0 21 int cap;
vferman 1:39e36a5c11e0 22 int weights[ARRAY_SIZE];
vferman 1:39e36a5c11e0 23 int values[ARRAY_SIZE];
vferman 1:39e36a5c11e0 24 int ts_begin;
vferman 1:39e36a5c11e0 25 int ts_end;
vferman 1:39e36a5c11e0 26 int proc_time;
vferman 1:39e36a5c11e0 27 int n;
vferman 1:39e36a5c11e0 28 }problema;
vferman 0:1ea8993f6b18 29
vferman 1:39e36a5c11e0 30 typedef struct{
vferman 0:1ea8993f6b18 31 int tid;
vferman 0:1ea8993f6b18 32 int pid;
vferman 0:1ea8993f6b18 33 int cap;
vferman 1:39e36a5c11e0 34 int weights[ARRAY_SIZE];
vferman 1:39e36a5c11e0 35 int values[ARRAY_SIZE];
vferman 0:1ea8993f6b18 36 int ts_begin;
vferman 0:1ea8993f6b18 37 int ts_end;
vferman 0:1ea8993f6b18 38 int proc_time;
vferman 1:39e36a5c11e0 39 int opt;
vferman 1:39e36a5c11e0 40 }solucion;
vferman 1:39e36a5c11e0 41
vferman 1:39e36a5c11e0 42
vferman 1:39e36a5c11e0 43 Timer t;
vferman 1:39e36a5c11e0 44 DigitalOut led1(LED1); // Red
vferman 1:39e36a5c11e0 45 DigitalOut led2(LED2); // Green
vferman 1:39e36a5c11e0 46 DigitalOut led3(LED3); // Blue
vferman 1:39e36a5c11e0 47
vferman 1:39e36a5c11e0 48 osMutexDef (mutex_leds);//mutex for LEDs
vferman 1:39e36a5c11e0 49 osMutexDef (mutex_p); //problem queue mutex
vferman 1:39e36a5c11e0 50 osMutexDef (mutex_s); //solution queue mutex
vferman 0:1ea8993f6b18 51
vferman 1:39e36a5c11e0 52
vferman 1:39e36a5c11e0 53 osMutexId (mutex_leds_id); // LED mutex ID
vferman 1:39e36a5c11e0 54 osMutexId (mutex_p_id); // problem queue mutex ID
vferman 1:39e36a5c11e0 55 osMutexId (mutex_s_id); // solution queue mutex ID
vferman 1:39e36a5c11e0 56
vferman 1:39e36a5c11e0 57
vferman 1:39e36a5c11e0 58 queue <problema> problem_q;
vferman 1:39e36a5c11e0 59 queue <solucion> solution_q;
vferman 1:39e36a5c11e0 60
vferman 1:39e36a5c11e0 61 Serial pc(USBTX, USBRX);
vferman 1:39e36a5c11e0 62
vferman 1:39e36a5c11e0 63 #ifdef DEBUG
vferman 1:39e36a5c11e0 64 int printP(problema p){
vferman 0:1ea8993f6b18 65 int i;
vferman 1:39e36a5c11e0 66 printf("\n#P \t#T \tCap \t#items");
vferman 1:39e36a5c11e0 67 printf("\n%i \t%i \t%i \t%i\n",p.pid,p.tid,p.cap,p.n);
vferman 1:39e36a5c11e0 68 printf(" Weights \tValues\n");
vferman 1:39e36a5c11e0 69 printf(" ------- \t------\n");
vferman 0:1ea8993f6b18 70 i=0;
vferman 1:39e36a5c11e0 71 while (p.weights[i]>-5 && p.values[i]>-5 && i<30){
vferman 1:39e36a5c11e0 72 printf("%i. %i \t%i\n", i+1, p.weights[i],p.values[i]);
vferman 0:1ea8993f6b18 73 i++;
vferman 0:1ea8993f6b18 74 }
vferman 1:39e36a5c11e0 75 printf("\n");
vferman 1:39e36a5c11e0 76 return 1;
vferman 1:39e36a5c11e0 77 }
vferman 1:39e36a5c11e0 78
vferman 1:39e36a5c11e0 79 int printS(solucion s){
vferman 1:39e36a5c11e0 80 int i;
vferman 1:39e36a5c11e0 81 printf("\n#S \t#T \tCap \tOpt Val \tBegin \t\tEnd \t\tProc");
vferman 1:39e36a5c11e0 82 printf("\n%i \t%i \t%i \t%i \t\t%ius \t%ius \t%ius\n", s.pid, s.tid, s.cap, s.opt, s.ts_begin, s.ts_end, s.proc_time);
vferman 1:39e36a5c11e0 83 printf(" Weights \tValues \t(included items)\n");
vferman 1:39e36a5c11e0 84 printf(" ------- \t------\n");
vferman 1:39e36a5c11e0 85 i=0;
vferman 1:39e36a5c11e0 86 while (s.weights[i]>-5 && s.values[i]>-5 && i<30){
vferman 1:39e36a5c11e0 87 printf("%i. %i \t%i\n", i+1, s.weights[i],s.values[i]);
vferman 1:39e36a5c11e0 88 i++;
vferman 1:39e36a5c11e0 89 }
vferman 1:39e36a5c11e0 90 printf("\n");
vferman 0:1ea8993f6b18 91 return 1;
vferman 0:1ea8993f6b18 92 }
vferman 1:39e36a5c11e0 93
vferman 1:39e36a5c11e0 94 #else
vferman 1:39e36a5c11e0 95 #endif
vferman 1:39e36a5c11e0 96
vferman 1:39e36a5c11e0 97 int printF(solucion s){
vferman 1:39e36a5c11e0 98 int i;
vferman 1:39e36a5c11e0 99 printf("\n#");
vferman 1:39e36a5c11e0 100 printf("%i,", s.tid);
vferman 1:39e36a5c11e0 101 printf("%i,", s.pid);
vferman 1:39e36a5c11e0 102 printf("%i", s.cap);
vferman 1:39e36a5c11e0 103 i=0;
vferman 1:39e36a5c11e0 104 while (s.weights[i]>=0 && s.values[i]>=0 && i<30){
vferman 1:39e36a5c11e0 105 printf(",%i,%i", s.weights[i],s.values[i]);
vferman 1:39e36a5c11e0 106 i++;
vferman 1:39e36a5c11e0 107 }
vferman 1:39e36a5c11e0 108 printf(",%i", s.ts_begin);
vferman 1:39e36a5c11e0 109 printf(",%i", s.ts_end);
vferman 1:39e36a5c11e0 110 printf(",%i", s.proc_time);
vferman 1:39e36a5c11e0 111 printf("*");
vferman 1:39e36a5c11e0 112 return 1;
vferman 1:39e36a5c11e0 113 }
vferman 1:39e36a5c11e0 114
vferman 0:1ea8993f6b18 115 int my_atoi(char *str) {
vferman 0:1ea8993f6b18 116 int res = 0; // Initialize result
vferman 0:1ea8993f6b18 117 // Iterate through all characters of input string and update result
vferman 0:1ea8993f6b18 118 for (int i = 0; str[i] != '\0'; ++i)
vferman 0:1ea8993f6b18 119 res = res*10 + str[i] - '0';
vferman 0:1ea8993f6b18 120 return res;
vferman 0:1ea8993f6b18 121 }
vferman 0:1ea8993f6b18 122
vferman 1:39e36a5c11e0 123 // A utility function that returns maximum of two integers
vferman 1:39e36a5c11e0 124 int max(int a, int b) { return (a > b)? a : b; }
vferman 1:39e36a5c11e0 125
vferman 1:39e36a5c11e0 126 // Returns the maximum value that can be put in a knapsack of capacity W
vferman 1:39e36a5c11e0 127 solucion knapSackP(problema p, int t){
vferman 1:39e36a5c11e0 128 solucion s;
vferman 1:39e36a5c11e0 129 s.ts_begin=t;//t.read_us();
vferman 1:39e36a5c11e0 130 int i, w,index=0;
vferman 1:39e36a5c11e0 131 int K[p.n+1][p.cap+1];
vferman 1:39e36a5c11e0 132 // Build table K[][] in bottom up manner
vferman 1:39e36a5c11e0 133 for (i = 0; i <= p.n; i++){
vferman 1:39e36a5c11e0 134 for (w = 0; w <= p.cap; w++){
vferman 1:39e36a5c11e0 135 if (i==0 || w==0)
vferman 1:39e36a5c11e0 136 K[i][w] = 0;
vferman 1:39e36a5c11e0 137 else if (p.weights[i-1] <= w)
vferman 1:39e36a5c11e0 138 K[i][w] = max(p.values[i-1] + K[i-1][w-p.weights[i-1]], K[i-1][w]);
vferman 1:39e36a5c11e0 139 else
vferman 1:39e36a5c11e0 140 K[i][w] = K[i-1][w];
vferman 1:39e36a5c11e0 141 }
vferman 1:39e36a5c11e0 142 }
vferman 1:39e36a5c11e0 143 s.opt = K[p.n][p.cap];
vferman 1:39e36a5c11e0 144
vferman 1:39e36a5c11e0 145 w = p.cap;
vferman 1:39e36a5c11e0 146 for (i=p.n; i>0; i--){
vferman 1:39e36a5c11e0 147 if(K[i][w] != K[i-1][w]){
vferman 1:39e36a5c11e0 148 //printf("(p.weights=%i,val=%i)-> ",p.weights[i-1],val[i-1]);
vferman 1:39e36a5c11e0 149 s.weights[index]=p.weights[i-1];
vferman 1:39e36a5c11e0 150 s.values[index++]=p.values[i-1];
vferman 1:39e36a5c11e0 151 w=w-p.weights[i-1];
vferman 1:39e36a5c11e0 152 }
vferman 1:39e36a5c11e0 153 }
vferman 1:39e36a5c11e0 154 s.weights[index]=-5;
vferman 1:39e36a5c11e0 155 s.values[index]=-5;
vferman 1:39e36a5c11e0 156
vferman 1:39e36a5c11e0 157
vferman 1:39e36a5c11e0 158 return s;
vferman 1:39e36a5c11e0 159 }
vferman 1:39e36a5c11e0 160
vferman 0:1ea8993f6b18 161 void led_func(uint32_t n){
vferman 0:1ea8993f6b18 162 uint32_t temp = ~((n%7)+1);
vferman 0:1ea8993f6b18 163 led3 = temp&(uint32_t)1;
vferman 0:1ea8993f6b18 164 led2 = (temp&(uint32_t)2)>>1;
vferman 0:1ea8993f6b18 165 led1 = (temp&(uint32_t)4)>>2;
vferman 0:1ea8993f6b18 166 }
vferman 0:1ea8993f6b18 167
vferman 0:1ea8993f6b18 168 void led_thread(void const *argument)
vferman 0:1ea8993f6b18 169 {
vferman 0:1ea8993f6b18 170 uint32_t cont =0;
vferman 0:1ea8993f6b18 171 while (true) {
vferman 1:39e36a5c11e0 172 osMutexWait(mutex_leds_id, osWaitForever); //request lock
vferman 0:1ea8993f6b18 173 led_func(cont);
vferman 1:39e36a5c11e0 174 osMutexRelease(mutex_leds_id); //unlock
vferman 0:1ea8993f6b18 175 Thread::wait((uint32_t)argument );
vferman 0:1ea8993f6b18 176 cont++;
vferman 0:1ea8993f6b18 177 }
vferman 0:1ea8993f6b18 178 }
vferman 0:1ea8993f6b18 179
vferman 1:39e36a5c11e0 180 void worker_thread(void const *argument){
vferman 1:39e36a5c11e0 181 problema p;
vferman 1:39e36a5c11e0 182 solucion s;
vferman 1:39e36a5c11e0 183
vferman 0:1ea8993f6b18 184 while (true) {
vferman 1:39e36a5c11e0 185 osMutexWait(mutex_p_id, 1000);
vferman 1:39e36a5c11e0 186 if (!problem_q.empty()){
vferman 1:39e36a5c11e0 187 p = problem_q.front();
vferman 1:39e36a5c11e0 188 problem_q.pop();
vferman 1:39e36a5c11e0 189 osMutexRelease(mutex_p_id);
vferman 1:39e36a5c11e0 190
vferman 1:39e36a5c11e0 191 #ifdef DEBUG
vferman 1:39e36a5c11e0 192 p.tid = (uint32_t)argument;
vferman 1:39e36a5c11e0 193 printf("-------------------------------------------------------");
vferman 1:39e36a5c11e0 194 printP(p);
vferman 1:39e36a5c11e0 195 #endif
vferman 1:39e36a5c11e0 196
vferman 1:39e36a5c11e0 197 //printf("\nT1:%i\n",t.read_us());
vferman 1:39e36a5c11e0 198 s = knapSackP(p,t.read_us());
vferman 1:39e36a5c11e0 199 s.ts_end=t.read_us();
vferman 1:39e36a5c11e0 200 //printf("\nT2:%i\n",t.read_us());
vferman 1:39e36a5c11e0 201 s.proc_time=s.ts_end-s.ts_begin;
vferman 1:39e36a5c11e0 202 s.tid = (uint32_t)argument;
vferman 1:39e36a5c11e0 203 s.pid = p.pid;
vferman 1:39e36a5c11e0 204 s.cap = p.cap;
vferman 1:39e36a5c11e0 205
vferman 1:39e36a5c11e0 206 #ifdef DEBUG
vferman 1:39e36a5c11e0 207 printS(s);
vferman 1:39e36a5c11e0 208 #endif
vferman 1:39e36a5c11e0 209
vferman 1:39e36a5c11e0 210 osMutexWait(mutex_s_id, 1000);
vferman 1:39e36a5c11e0 211 solution_q.push(s);
vferman 1:39e36a5c11e0 212 osMutexRelease(mutex_s_id);
vferman 1:39e36a5c11e0 213
vferman 1:39e36a5c11e0 214 osMutexWait(mutex_leds_id, 1000); //request lock
vferman 1:39e36a5c11e0 215 led_func((uint32_t)argument);
vferman 1:39e36a5c11e0 216 osMutexRelease(mutex_leds_id); //unlock
vferman 1:39e36a5c11e0 217
vferman 1:39e36a5c11e0 218 }else{
vferman 1:39e36a5c11e0 219 osMutexRelease(mutex_p_id);
vferman 1:39e36a5c11e0 220 }
vferman 0:1ea8993f6b18 221
vferman 1:39e36a5c11e0 222 osThreadYield();
vferman 1:39e36a5c11e0 223 #ifdef DELAY_INSIDE
vferman 1:39e36a5c11e0 224 Thread::wait(DELAY_INSIDE_TIME);
vferman 1:39e36a5c11e0 225 #endif
vferman 0:1ea8993f6b18 226 }
vferman 0:1ea8993f6b18 227 }
vferman 0:1ea8993f6b18 228
vferman 0:1ea8993f6b18 229 int main(){
vferman 1:39e36a5c11e0 230 t.start();
vferman 1:39e36a5c11e0 231 problema *problemas_array; //array to store the parsed problems
vferman 1:39e36a5c11e0 232 Thread *threads_array[THREAD_ARRAY_SIZE]; //array to instance the threads
vferman 1:39e36a5c11e0 233 char buffer[4]; //buffer to atoi convertion
vferman 1:39e36a5c11e0 234 solucion s; // var to pop the solutions from solution queue
vferman 1:39e36a5c11e0 235 char input_char; //received char form serial input
vferman 1:39e36a5c11e0 236 char stage = 0; //State of the parsing loop
vferman 1:39e36a5c11e0 237 char parseFlag =1; //flag to exit the input loop
vferman 1:39e36a5c11e0 238 char displayFlag =1; //flag to exit the display solution loop
vferman 1:39e36a5c11e0 239 int i; //used to indexing alog the differents arrays
vferman 1:39e36a5c11e0 240 int number_threads; //number of threads recived
vferman 1:39e36a5c11e0 241 int number_threads_temp; //used to verify if we have recived the number of threads
vferman 1:39e36a5c11e0 242 char problema_counter; //problem counter
vferman 1:39e36a5c11e0 243 char item_counter; //
vferman 1:39e36a5c11e0 244 char buffer_counter; //used to indexing the buffer used to atoi convertion
vferman 1:39e36a5c11e0 245 pc.baud (115200); //Baudrate
vferman 0:1ea8993f6b18 246
vferman 0:1ea8993f6b18 247
vferman 0:1ea8993f6b18 248 while (true) {
vferman 1:39e36a5c11e0 249 problemas_array =(problema *) malloc(10*sizeof(problema)) ;
vferman 1:39e36a5c11e0 250 problema_counter=0;
vferman 1:39e36a5c11e0 251 number_threads=0;
vferman 1:39e36a5c11e0 252 item_counter=0;
vferman 1:39e36a5c11e0 253 buffer_counter=0;
vferman 1:39e36a5c11e0 254 buffer[0]='\0';
vferman 1:39e36a5c11e0 255 buffer[3]='\0';
vferman 1:39e36a5c11e0 256 i=0;
vferman 0:1ea8993f6b18 257 parseFlag=1;
vferman 1:39e36a5c11e0 258 displayFlag =1;
vferman 1:39e36a5c11e0 259 stage = 0;
vferman 1:39e36a5c11e0 260
vferman 1:39e36a5c11e0 261 while(parseFlag){ //input loop
vferman 1:39e36a5c11e0 262 input_char = getchar(); //receive char from Serial
vferman 1:39e36a5c11e0 263 //Parsing the message
vferman 1:39e36a5c11e0 264 if (input_char=='&'){ //start of message
vferman 0:1ea8993f6b18 265 buffer[0]='\0';
vferman 0:1ea8993f6b18 266 buffer_counter=0;
vferman 0:1ea8993f6b18 267 problema_counter=0;
vferman 0:1ea8993f6b18 268 item_counter=0;
vferman 0:1ea8993f6b18 269 number_threads=0;
vferman 0:1ea8993f6b18 270 stage = 1;
vferman 1:39e36a5c11e0 271 //printf("Stage %i\n", stage);
vferman 1:39e36a5c11e0 272 }else if (input_char=='#'){ //end of threads number
vferman 0:1ea8993f6b18 273 buffer[0]='\0';
vferman 0:1ea8993f6b18 274 buffer_counter=0;
vferman 0:1ea8993f6b18 275 stage = 2;
vferman 1:39e36a5c11e0 276 //printf("Stage %i\n", stage);
vferman 1:39e36a5c11e0 277 }else if (input_char >= '0' && input_char <= '9'){ //number received
vferman 0:1ea8993f6b18 278 buffer[buffer_counter++]=input_char;
vferman 0:1ea8993f6b18 279 buffer[buffer_counter]='\0';
vferman 1:39e36a5c11e0 280 }else if (input_char=='*'){ //end of line
vferman 0:1ea8993f6b18 281 if (stage==1){
vferman 1:39e36a5c11e0 282 number_threads_temp = my_atoi(&buffer[0]);
vferman 1:39e36a5c11e0 283 number_threads = (number_threads_temp>0 && number_threads_temp<=10)? number_threads_temp: 5;
vferman 1:39e36a5c11e0 284 //printf("Number of threads = %i\n", number_threads);
vferman 1:39e36a5c11e0 285 //printf("Problema = %i\n", problema_counter);
vferman 0:1ea8993f6b18 286 buffer[0]='\0';
vferman 0:1ea8993f6b18 287 buffer_counter=0;
vferman 0:1ea8993f6b18 288 }else if (stage==4){
vferman 1:39e36a5c11e0 289 problemas_array[problema_counter].values[item_counter++]=my_atoi(&buffer[0]);
vferman 0:1ea8993f6b18 290 problema_counter++;
vferman 0:1ea8993f6b18 291 item_counter=0;
vferman 1:39e36a5c11e0 292 //printf("4Problema = %i\n", problema_counter);
vferman 0:1ea8993f6b18 293 }else if (stage==5){
vferman 1:39e36a5c11e0 294 problemas_array[problema_counter].values[item_counter++]=my_atoi(&buffer[0]);
vferman 1:39e36a5c11e0 295 problemas_array[problema_counter].values[item_counter]=-5;
vferman 1:39e36a5c11e0 296 problemas_array[problema_counter].weights[item_counter]=-5;
vferman 1:39e36a5c11e0 297 problemas_array[problema_counter].n=item_counter;
vferman 0:1ea8993f6b18 298 problema_counter++;
vferman 0:1ea8993f6b18 299 item_counter=0;
vferman 1:39e36a5c11e0 300 //printf("Problema = %i\n", problema_counter);
vferman 0:1ea8993f6b18 301 }
vferman 0:1ea8993f6b18 302
vferman 1:39e36a5c11e0 303 }else if (input_char==','){ //delimiter do the message
vferman 0:1ea8993f6b18 304 if (stage==2){
vferman 1:39e36a5c11e0 305 problemas_array[problema_counter].pid = my_atoi(&buffer[0]);
vferman 0:1ea8993f6b18 306 stage=3;
vferman 1:39e36a5c11e0 307 //printf("Stage %i\n", stage);
vferman 0:1ea8993f6b18 308 }else if (stage==3){
vferman 1:39e36a5c11e0 309 problemas_array[problema_counter].cap = my_atoi(&buffer[0]);
vferman 0:1ea8993f6b18 310 stage=4;
vferman 1:39e36a5c11e0 311 //printf("Stage %i\n", stage);
vferman 0:1ea8993f6b18 312 } else if (stage==4){
vferman 1:39e36a5c11e0 313 problemas_array[problema_counter].weights[item_counter]=my_atoi(&buffer[0]);
vferman 0:1ea8993f6b18 314 stage=5;
vferman 1:39e36a5c11e0 315 //printf("Stage %i\n", stage);
vferman 0:1ea8993f6b18 316 }else if (stage==5){
vferman 1:39e36a5c11e0 317 problemas_array[problema_counter].values[item_counter++]=my_atoi(&buffer[0]);
vferman 0:1ea8993f6b18 318 stage=4;
vferman 1:39e36a5c11e0 319 //printf("Stage %i\n", stage);
vferman 0:1ea8993f6b18 320 }
vferman 0:1ea8993f6b18 321 buffer[0]='\0';
vferman 0:1ea8993f6b18 322 buffer_counter=0;
vferman 1:39e36a5c11e0 323 }else if (input_char=='\n'){ //newline received
vferman 0:1ea8993f6b18 324 buffer[0]='\0';
vferman 0:1ea8993f6b18 325 buffer_counter=0;
vferman 1:39e36a5c11e0 326 }else if (input_char=='X'){ //end of message
vferman 0:1ea8993f6b18 327 buffer[0]='\0';
vferman 0:1ea8993f6b18 328 buffer_counter=0;
vferman 0:1ea8993f6b18 329 stage=0;
vferman 1:39e36a5c11e0 330 //printf("\nNumber of threads = %i\n", number_threads);
vferman 1:39e36a5c11e0 331 fflush(stdout);
vferman 1:39e36a5c11e0 332 fflush(stdin);
vferman 1:39e36a5c11e0 333 parseFlag =0; //exit form input loop
vferman 0:1ea8993f6b18 334 }
vferman 0:1ea8993f6b18 335 }
vferman 1:39e36a5c11e0 336 //printf("\nNumber of threads = %i", number_threads);
vferman 1:39e36a5c11e0 337 //printf("\nNumber of problems = %i", problema_counter);
vferman 1:39e36a5c11e0 338 printf("\n Parse Finished \n");
vferman 1:39e36a5c11e0 339
vferman 1:39e36a5c11e0 340 osMutexWait(mutex_p_id, osWaitForever); //request lock
vferman 1:39e36a5c11e0 341 for (i=0; i<problema_counter; i++){
vferman 1:39e36a5c11e0 342 problem_q.push(problemas_array[i]);
vferman 1:39e36a5c11e0 343 }
vferman 1:39e36a5c11e0 344 osMutexRelease(mutex_p_id); //unlock
vferman 1:39e36a5c11e0 345
vferman 1:39e36a5c11e0 346 free(problemas_array);
vferman 1:39e36a5c11e0 347
vferman 1:39e36a5c11e0 348 i=0;
vferman 1:39e36a5c11e0 349 while (i<number_threads){
vferman 1:39e36a5c11e0 350 threads_array[i]= new Thread(worker_thread,(void *)(i));
vferman 1:39e36a5c11e0 351 #ifdef DELAY_BTWN
vferman 1:39e36a5c11e0 352 Thread::wait(DELAY_BTWN_TIME);
vferman 1:39e36a5c11e0 353 #endif
vferman 1:39e36a5c11e0 354 i++;
vferman 1:39e36a5c11e0 355 }
vferman 1:39e36a5c11e0 356
vferman 1:39e36a5c11e0 357 #ifdef DELAY_DISPLAY
vferman 1:39e36a5c11e0 358 Thread::wait(DELAY_DISPLAY_TIME);
vferman 1:39e36a5c11e0 359 #endif
vferman 1:39e36a5c11e0 360
vferman 1:39e36a5c11e0 361
vferman 1:39e36a5c11e0 362 while(displayFlag){
vferman 1:39e36a5c11e0 363 displayFlag=0;
vferman 1:39e36a5c11e0 364 printf("\n&%i*",number_threads);
vferman 1:39e36a5c11e0 365
vferman 1:39e36a5c11e0 366 while(!solution_q.empty()){
vferman 1:39e36a5c11e0 367 osMutexWait(mutex_s_id, osWaitForever);
vferman 1:39e36a5c11e0 368 if (!solution_q.empty()){
vferman 1:39e36a5c11e0 369 s = solution_q.front();
vferman 1:39e36a5c11e0 370 solution_q.pop();
vferman 1:39e36a5c11e0 371 printF(s);
vferman 1:39e36a5c11e0 372 }
vferman 1:39e36a5c11e0 373 osMutexRelease(mutex_s_id);
vferman 1:39e36a5c11e0 374 }
vferman 1:39e36a5c11e0 375
vferman 1:39e36a5c11e0 376
vferman 1:39e36a5c11e0 377 }
vferman 1:39e36a5c11e0 378
vferman 1:39e36a5c11e0 379
vferman 1:39e36a5c11e0 380 //i=0;
vferman 1:39e36a5c11e0 381 //while (i<number_threads) threads_array[i];
vferman 1:39e36a5c11e0 382 free(threads_array);
vferman 1:39e36a5c11e0 383
vferman 1:39e36a5c11e0 384 //osMutexWait(mutex_leds_id, osWaitForever); //request lock
vferman 1:39e36a5c11e0 385 // led_func(6);
vferman 1:39e36a5c11e0 386 //osMutexRelease(mutex_leds_id); //unlock
vferman 0:1ea8993f6b18 387 }
vferman 0:1ea8993f6b18 388 }