i am programming a concurrent (multi-threaded) server application - using pthread, of cource!- when i encountered a problem with pthread_join, my app is shuch (pseudo-code):
/* Sheduler thread */
while (1) {
c = get_client();
r = get_resource();
communication.c = c;
communication.r = r;
pthread_create( clnt_tid, null, clnt_fn, communication);
resource_alloc_list_add(clnt_tid, r);
}
/* resources Collector thread */
while(!rs_alloc_list_is_empty()) {
e = get_elt(rs_alloc);
pthread_join(e.clnt_tid, retour);
free_rs(e.r);
}
.........
the problem is that there is no non-bloquing pthread_join call - there is one but it isn't portable - and pthread_join can't join any thread like wait() in process programming . So if the rsc_collector thread is waiting for one client thread to exit to get back the allocated resource ,and before this happens all other threads have exited then their resource will be blocked - and the scheduler thread can't serve other client - until the first thread terminates his work . can you tell-me a possible solution to this problem ?
EDIT :
I'll be more specific , i am programming an local resource management system (lrms), or Remote Program execution system, there is three different programs : client pg, server pg, and scheduler pg, the client contact the scheduler and wait for scheduler pg to allocate him an idle server so he can after submit, execute his job on an remote server.the sched pg will en-queue the client address in the clients queue. in the other side , a server pg send an registration message to scheduler and wait for job, the scheduler enqueue the server address in the resources queue (so i mean by resource an remote server not resources allocated to the thread by system ). the scheduler pg consists of three principal threads :
- main_thread : listing on socket binded at Well Known Port. receives and en-queue demands and registrations (In other word the Producer)
- scheduler_thread : the consumer , de-queue cliets address get server (dequeue an server address), create the client thread ,save the client/server allocation. and loop on.
- resource_collector thread : wait for clients threads to terminates to get back the allocated resource. i emphasize [*] to create a specific thread for resource collecting because it is very important for the proper fonctionnement of the system.
Note :
- the client thread get an copy of the resource (server) not the original.
- [*] if i eliminate that thread , and let each client free its esource (remote server address) so in some cases when an client thread crashes before the call to the free function , then the resource will be lost for ever ... that's why i wouldn't risk letting the resources under the full control of the clients threads.