Threads problem
-
I have a problem with Threads !In a console application ,I am trying to create 10 threads using CreateThread(..). Each Thread created tries to call the interface function which will add records to database !The problem is I have given separate ThreadProcs for each thread .But, what happens is some threads log more than once.So, the loggings of some threads are lost !But, teh number of loggings are correct !How can I rectify the problem ! The Mutexes need not do anything here .But, i can get the correct result only if I use Mutex at the start of each ThreadProc .It is logically wrong !How can i rectify the problem ! Thanx in advance for the help ! Regards, yamini Y.Yamini Devi.
-
I have a problem with Threads !In a console application ,I am trying to create 10 threads using CreateThread(..). Each Thread created tries to call the interface function which will add records to database !The problem is I have given separate ThreadProcs for each thread .But, what happens is some threads log more than once.So, the loggings of some threads are lost !But, teh number of loggings are correct !How can I rectify the problem ! The Mutexes need not do anything here .But, i can get the correct result only if I use Mutex at the start of each ThreadProc .It is logically wrong !How can i rectify the problem ! Thanx in advance for the help ! Regards, yamini Y.Yamini Devi.
First of all, you should be using _beginthread() instead of CreateThread(). CreateThread() is a Win32 call and does not initialize the C Run Time library. Second, you need to mutex protect all shared resources that are not thread safe. I don't know what your "logging" refers to, but if it is not thread safe, you need to mutex protect calls to it. Chris Hafey
-
First of all, you should be using _beginthread() instead of CreateThread(). CreateThread() is a Win32 call and does not initialize the C Run Time library. Second, you need to mutex protect all shared resources that are not thread safe. I don't know what your "logging" refers to, but if it is not thread safe, you need to mutex protect calls to it. Chris Hafey
Thank you for the help.I tried with _beginthread().I have used mutexes to make the code Thread safe .Still, I am getting the same problem.I tried terminating the Thread with _endthread() also.But the same problem persists.What can be done ?Where am I going wrong ! I have given the code I have used for each thread. *************************************************************** HANDLE h[10]; void ThreadProc(void*); void main() { h[0]=(void*)_beginthread(ThreadProc1,0,0); //Likewise All other 9 threads are created here . WaitForMultipleObjects(5,h,TRUE,INFINITE); } void ThreadProc1(void* p) { //code for logging into database goes here _endthread(); } **************************************************************** Help me to rectify the problem . Y.Yamini Devi
-
Thank you for the help.I tried with _beginthread().I have used mutexes to make the code Thread safe .Still, I am getting the same problem.I tried terminating the Thread with _endthread() also.But the same problem persists.What can be done ?Where am I going wrong ! I have given the code I have used for each thread. *************************************************************** HANDLE h[10]; void ThreadProc(void*); void main() { h[0]=(void*)_beginthread(ThreadProc1,0,0); //Likewise All other 9 threads are created here . WaitForMultipleObjects(5,h,TRUE,INFINITE); } void ThreadProc1(void* p) { //code for logging into database goes here _endthread(); } **************************************************************** Help me to rectify the problem . Y.Yamini Devi