Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Threads problem

Threads problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasequestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yamini
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • Y yamini

      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.

      C Offline
      C Offline
      Chris Hafey
      wrote on last edited by
      #2

      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

      Y 1 Reply Last reply
      0
      • C 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

        Y Offline
        Y Offline
        yamini
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • Y yamini

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Last time I checked, the thread will exit on its own if you leave (return from) the thread function. There is no need to call _endthread() from within the thread itself.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups