[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Terminering af tråde



Hej.

Jeg har et spørgsmål som nok er i klassen 'hvad er best practice'.

Spørgsmålet er, hvordan termineres ventende tråde bedst ?

Antag vi har et program, på Linux eller måske Un*x. Programmet benytter tråde, der f.eks. startes med libSDL.

Antag nu at vi har følgende løse syntaks angivet i programmet længere nede. Hvordan er det at man mest korrekt får sinaleret til thread_2 at den skal termineres ?

Som det er angivet i koden, termineres (kill) en tråd, der sandsynligvis er blokeret eller venter på at returnere fra et kald til read eller write. Giver det ikke problemer ? Det ville jo være bedst at få read() til at terminere, få skrevet det der skal (write()) og så returneret thread_2, det vil sige den slutter selv og thread_1 kunne så pænt vente med et thread_wait og opsamle returværdien.

Hvis jeg blot terminerer en tråd, der er blokeret af f.eks. read() eller write(), hvornår kan jeg så lave en close på fd ? Og risikerer jeg ikke at der er ressourcer, der ikke er ryddet ordentlig op?

mvh.

Peter



Thread_1:
  run2 = true; fd1=-1; fd2=-1;
  while (1) {
    if (fd1==-1) fd1 = open(...);        // socket/file/whatever
    if (fd2==-1) fd2 = open(...);
    Create thread_2
    Go do something for a while
    ...
    if (pigs can fly) break;
    Go do something for a while
    ...
    if (something happened) {
      Terminate Thread_2;
      close(fd1); fd1=-1;
      close(fd2); fd2=-1;
    }
  }


Thread_2
  while (run2) {
    if ((n=read(fd1,.....)) < 1) break;
    write(fd2,...,n);
  }
  return status;


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2008-04-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *