How to access a single object from multiple thread
-
:doh: Hello friends I am using VC++ 6.0 for writing a multi threaded application. But I am facing problems when I try to Update a recordset (CRecordset object) from multiple threads running simultaneously please help Ranojay
Have a look at critical sections - they could be your friend.
CreateCriticalSection EnterCriticalSection LeaveCriticalSection DestroyCriticalSection
(? - could be delete) Good luck, Iain. -
Have a look at critical sections - they could be your friend.
CreateCriticalSection EnterCriticalSection LeaveCriticalSection DestroyCriticalSection
(? - could be delete) Good luck, Iain. -
Thank you Sir for your help But the functions
EnterCriticalSection LeaveCriticalSection
are giving error messages while in use can you be more specific about how to use them thanks againI have *never* had one of those functions giving me an error. The only thing I can think of is that you've not initialised the critical section before using it. Look on MSDN for EnterCriticalSection. One of the links take you to an example page for critical sections. (Copied below) I would avoid the use of a global variable, and make the critical section a co-member of a class with your recordset, but I hope you'll get the idea. Iain.
// Global variable
CRITICAL_SECTION CriticalSection;void main()
{
...// Initialize the critical section one time only. InitializeCriticalSection(&CriticalSection); ... // Release resources used by the critical section object. DeleteCriticalSection(&CriticalSection)
}
DWORD WINAPI ThreadProc( LPVOID lpParameter )
{
...// Request ownership of the critical section. \_\_try { EnterCriticalSection(&CriticalSection); // Access the shared resource. } \_\_finally { // Release ownership of the critical section. LeaveCriticalSection(&CriticalSection); } ...
}
-
I have *never* had one of those functions giving me an error. The only thing I can think of is that you've not initialised the critical section before using it. Look on MSDN for EnterCriticalSection. One of the links take you to an example page for critical sections. (Copied below) I would avoid the use of a global variable, and make the critical section a co-member of a class with your recordset, but I hope you'll get the idea. Iain.
// Global variable
CRITICAL_SECTION CriticalSection;void main()
{
...// Initialize the critical section one time only. InitializeCriticalSection(&CriticalSection); ... // Release resources used by the critical section object. DeleteCriticalSection(&CriticalSection)
}
DWORD WINAPI ThreadProc( LPVOID lpParameter )
{
...// Request ownership of the critical section. \_\_try { EnterCriticalSection(&CriticalSection); // Access the shared resource. } \_\_finally { // Release ownership of the critical section. LeaveCriticalSection(&CriticalSection); } ...
}
Thanks again I will try that. Actaully I am using the CWinThread Run() function from a CWinThread derived class and doing the simultaneous update of the CRecordSet object and not AfxBeginthread function will that work too? anyway thank you very much for your help and I will keep you disturbing Thanks a lot
-
Thanks again I will try that. Actaully I am using the CWinThread Run() function from a CWinThread derived class and doing the simultaneous update of the CRecordSet object and not AfxBeginthread function will that work too? anyway thank you very much for your help and I will keep you disturbing Thanks a lot
The code I showed was just an example - mostly for initialising the critical section. Any code wrapped up by a Enter/LeaveCriticalSection pair will be callable from only one thread at a time. This includes any calls you make to a database, etc. It doesn't matter whether you make a thread using CWinThread::Run, AfxBeginThread, or ::CreateThread (mostly because they all end up being a call to BeginThread). Iain.
-
The code I showed was just an example - mostly for initialising the critical section. Any code wrapped up by a Enter/LeaveCriticalSection pair will be callable from only one thread at a time. This includes any calls you make to a database, etc. It doesn't matter whether you make a thread using CWinThread::Run, AfxBeginThread, or ::CreateThread (mostly because they all end up being a call to BeginThread). Iain.
Sir I tried all the functions but it is still not working some rows are surely getting updated but some of them are not the error Mesage is
No rows were affected by the update or delete operation.
has it any thing to do with the type of database I am using.Actually I am uising MS-ACCESS database. may be ACCESS is not able to handle concurrent updates