(workaround) Thread Status ermitteln
-
Hallo Zusammen,
ich möchte mehrere Threads verwalten, welche jeweils kurzzeitig
Clientanfragen beantworten über ein Socket.Da ich bei dem Starten eines Threads eine Thread Struktur mitgegeben
muss, muss ich wissen, ob das besagte Element der Struktur frei
ist nun nicht durch einen laufenden Thread belegt ist.Ich arbeite mit Detach_thread. Das Abfragen über join entfällt also.
Hat jemand eine Idee. Mein erster Ansatz war, das ich die ThreadID
in einer Arraystruktur speichere und auf 0 vergleiche.Ich denke aber das das einfacher geht mit Bordmitteln.
Edit:
Ich habe keine Lösung mit Boardmitteln gefunden.
Ich konnte keine Funktion finden, welche mir:
- den Status (läuft, beendet)
- Hängt am Mutex oder so.
finden.Ich habe mir erstmal mit folgendem Strukt geholfen, vielleicht
finde ich die andere Info später noch.Definitionen:
typedef struct threadInfo { long socket; long threadId; } _threadInfo; #define MAX_THREADS 5 pthread_t th[MAX_THREADS]; /* Thread control structure */ struct threadInfo thread_info[MAX_THREADS];
Socket routine
static void *socket_request (void *arg) { ... ... struct threadInfo *Infos; Infos = (struct threadInfo*)arg; /* Convert pointer to local variable */ sockethandle = Infos->socket; /* Convert thread argument to int handle */ Infos->threadId = pthread_self (); /* store process ID */ pthread_detach (pthread_self ()); /* disconnect thread from main task */ .... .... close (sockethandle); /* Close the socket connection */ Infos->threadId=0; /* clear thread ID information */ Infos->socket=0; /* Clear socket information */ }
Prüfroutine für den neu zu startenden Thread
for(i=0;i<MAX_THREADS;i++) { if( thread_info[i].threadId == 0 && thread_info[i].socket == 0) /* Thread ID clear ?*/ { thread_info[i].socket=(long) new_socket; /* Give thread the socket information */ ret = pthread_create( &th[i], NULL, &socket_request, (void*)&thread_info[i]); /* Create a thread for the request */ break; } }
Ich hoffe jemand finde eine bessere Möglichkeit hierfür. Ich weiss derzeit keine bessere.
Gruss