⓪編著 :蕭沖

Thread Pool
1/ Create map container of thread data MAY including idle property for later SetEvent, event handle property for Reset event later, thread handle property for close handle later .....by thread id as its key.
2/ Create WorkItem Queue (list for insert priority item or deque for normal queue) data with object,parameter pointer,function pointer.....
3/ Create thread function with thread map container parameter. Impelement function as : get event handler by thread id key from container, WaitForMultipleObjects for handler just get and more utra event handler like shutdown notify. Or using simple semaphore mechanism. Get WorkItem from Queue, and call funtion. Keep looping...
4/ Create threads and insert each thread data into map.
5/ Queue the WorkItem and set event handler for each idle thread in map.

One thread can create its own event object, and be controled after queue work.
Semaphore is a global object, and every thread check it to see if it's count greater than 0,
if yes, then go work, if equal to 0, then keep waiting....

Multithread Socket programming
1/ normal model is after listening imcoming request.... loop for accept call.
2/ accept function is a bloking function, ie, if there's no request, it won't return, for this reason,
process beter create one thread to create server socket and binding and listening....
3/ when accept call returned, it create a new client socket for further communication with client.
4/ create a thread to deal with this new client socket for sending msg or rcv msg....

Traditional model is just like above, it using one thread per connection. and dynamically create thread.
Threads are created and then terminated, cycling....but this may cause taking much overhead.
Using thread pool for deal with client sockets is a good ideal for saving resource.

PS.The other model is using IOCP model for more robust and scalable....
arrow
arrow
    全站熱搜

    aftcast 發表在 痞客邦 留言(0) 人氣()