Passing COM interface pointers between threads is a somewhat complicated affair. This page on multi-threading and COM, although Delphi centric, provides a good overview. This codeguru article on COM threading may also be helpful. You can get more information via a search on COM threading.

Basically, when you wish to pass an interface pointer between threads you must marshal it. There are a couple of "simple" methods to do this. The first is the briefly named CoMarshalInterThreadInterfaceInStream()/CoGetInterfaceAndReleaseStream() functions. There are also ATL wrappers for these functions.

The other method is to use a global interface table in the form of IGlobalInterfaceTable. You add an interface using RegisterInterfaceInGlobal() and can retrieve the interface from other threads using GetInterfaceFromGlobal(). This method may be simpler, especially when you have multiple interface pointers to marshal:

I am sorry to be ignorant. I used both registering the interface and used streams to send my recordset object. It is compiling properly but I am getting the same runtime error "abnormal Program termination". Please Help!!!

I am sorry to be ignorant. I used both registering the interface and used streams to send my recordset object. It is compiling properly but I am getting the same runtime error "abnormal Program termination". Please Help!!!

Well both should work (the Interface Table method definately as I ammended your code using tht and it worked)

Also, I would put the code between entering and leaving the critical section inside a try catch statement. It's probably a _com_error being thrown and not caught that's crashing your prog...this could happen with null interfaces (problem when sharing interface pointers between threads) but it could also happen if you are going past the EOF mark on the recordset (IE if the number of threads exceeds the number of records returned in the recordset)

First of all I would like to Thank Fordy and anonytmouse for their great help. after inserting the modifications you mentioned in my code it finally worked.

Now I am facing a different problem. for example if my database has 100 records and I create only 10 threads. the present program exits after retrieving 10 records and the remaining 90 records are not retrieved.

That is, My Multi-threading concept after executing the ThreadFunc() only once gets exited. Is there an way that my Multi-threading does not exit till my Recordset EOF is not reached. i.e. till my Recordset is not empty that it will continue to execute ThreadFunc()

Currently you are setting the number of threads in your for loop to 10 (NUM_THREADS)....why not se it to Recordset::RecordCount...that way each member of the recordset ill get a thread to access it.

>>Also Is there an possiblity that I can distinguish the threads.

Dont quite understand the meaning here....you can differentiate a thread by passing a unique param to the lpParameter argument of CreateThread...or you can store thread specific information using Thread Local Storage (Lookup TlsAlloc, TlsGetValue, TlsSetValue, TlsFree)

I'd consider doing all the database operations in the main thread and passing any information that is needed to the worker threads. This will provide a much simpler, more robust and higher performance solution. If you need to pass more than one value to the worker threads you can do so in a structure.

As for limiting the number of worker threads running at any one time you can use a semaphore. A semaphore keeps a count. Every time you wait on the semaphore the count is reduced by one. If the count is zero, the wait will return only when the count becomes greater than zero. You increment the count by calling ReleaseSemaphore(). So, to implement a maximum thread feature, you wait for the semaphore before creating a thread and release the semaphore when exiting a thread.

I went through semaphores. I still have some doubts. The main reason I am going for multi-threading is I wanted all the 48 telephone lines to call simultaneously.

I represent each telephone line to a single thread, but with my concept of Enter critical section and leave critical section. will my threads will call simultaneously or will wait for one thread to call and the other will resume after the first thread executes.

In the place of printf(), I will call the function to dialogic box which will make telephone call. as having critical sections will it cause the single thread to make call at a time and will other threads wait on for the thread which has control to make calls.

please correct me.

Also with semaphores, even though there will be 48 threads at any time. is it possible that the thread that was created first retrieves the tel. number from database and will die after making a call.

The main reason I am concerned is I wanted to know which tel. line called which number from database.