[En-Nut-Discussion] Multithreading

Fernando Me Ga ferradi at gmail.com
Mon Mar 29 14:19:22 CEST 2010


Hello,

I would like to know what happen in a multithread program executing
the same routins. What happen if in the middle of the execution of the
routing another thread get the CPU and strar executing the same
routing. In my example I can only create up to 6 thread at the same
time. The example is shown below.

Thank you for your help.

...
void GlobalRoutin (FILE * stream) {
  /* Read and write from stream using stdio routins */
}

THREAD(Server, arg) {
   FILE *stream;
   TCPSOCKET *sock = NutTcpCreateSocket();
   NutTcpAccept(sock, 23);
   NutThreadCreate("serv", Server, NULL, 512);
   if ((stream = _fdopen((int) sock, "r+b")) != 0) {
      GlobalRoutin (stream);
      fclose(stream);
   }
   NutTcpCloseSocket(sock);
   NutThreadKill();
   for (;;);
}

int main (void) {
   ...
  NutThreadCreate("serv", Server, NULL, 512);
  for (;;) {
        NutThreadYield();
   }
}


Start  |
        \/
        Thread1  |
                     \/
                     CallGlobalRoutin  |
                                              \/
                                              StartRoutin  |
                                                                \/
                                                                ***
(brake)  --> Thread2  |

                            \/

                            CallGlobalRoutin  |

                                                     \/

                                                     StartRoutin  |


\/

|<--------------- Return to Thread1 execution <-- (brake) ***
                                                                 \/
                                               EndRoutin
                                               |
                                              \/
                                              Give CPU to Thread 2
------------------------------------------>  |

                                                     \/

                                                     EndRoutin  |


\/


...


More information about the En-Nut-Discussion mailing list