how do i create thread safe worker thread 
Author Message
 how do i create thread safe worker thread

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.



Tue, 22 Feb 2005 19:55:01 GMT  
 how do i create thread safe worker thread

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.

Do you have any statistics on the maximum size your linked lists grow to?
If so, you might be able to improve performance by creating an array
of objects. This array would not require any compacting or periodic
memory deallocation. Simply treat your array as a circular buffer.

Jim Rogers



Tue, 22 Feb 2005 20:14:04 GMT  
 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


Wed, 23 Feb 2005 03:24:29 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. worker thread vs main thread?

2. COM Threading Model for ISAPI Worker Threads

3. Handling events from a worker thread in the main thread

4. component in main threads used in worker threads

5. Exe server threading model - events from worker thread question

6. How can i terminate a Worker Thread during a Thread is running?Thank

7. How to Send messages from worker thread to main thread

8. Synchronization problem between main thread and worker thread

9. UI Thread better than Worker thread for speed?

10. Determine if current thread is a worker thread

11. Posting a message from a worker thread to the main app thread

12. worker thread to ui thread

 

 
Powered by phpBB® Forum Software