
how do i create thread safe worker thread
Thread safety is a property of data, not of threads. Read my essay on my MVP Tips site on
semaphores to see how to build a suitable thread-safe queue. Therefore, you don't need
counters; the count is implicit in the semaphore. Essentially, this is one of the
exercises from my System Programming course. You can use either a Mutex or a
CRITICAL_SECTION for synchronization, depending on whether you need inter-process or
intra-process synchronization of the queue(s).
joe
Quote:
>I have an application (single threaded) that accepts incoming data and
>dispatches the data over to databases,etc. The problem is if one
>database is very slow, then it bogs everything down because the main
>thread waits while the database functions finish( success/fail,etc)..
>my new design idea:
>4 threads. main thread gets incoming data, and the data is sent to
>the 3 worker threads that process the data. no response is needed
>from these worker threads. so if thread #3 is slow, no big deal --
>the incoming queue just builds up for that queue until it is ready to
>process. but threads 1 and 2 are not forced to slow down.
>my problem: I create a globally accessible linked list for each thread
>(the linked lists are simple.. just a link of strings). i use global
>counters (int) so my threads know how many items (if any) are in the
>list to see if we need to process anything. everytime i
>add,remove,read from linked or counters, i put the code inside
>critical sections. I found out the hard way even reading a global int
>is not thread safe (i thought it would be atomic but what do i know,
>maybe it is the multi-processors?) i have created 3 separate critical
>sections one for each thread, but the problem is the main thread now
>is spending too much time waiting for these critical sections to free
>up. it doesnt take too long to read the integers, and copy what is
>needed from the lists, but compacting and freeing memory from the
>lists periodically (every few seconds) is what seems to slows things
>down a bit. performance is better than before, but not like I think it
>should or could be. I am also concerned if my design is really an
>acceptable or good method.
>question: what is a better design that is simple and efficient? all i
>want to do is pump data to these worker threads, process them, and the
>worker threads return nothing back to the main thread.
Joseph M. Newcomer [MVP]
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm