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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multithread question repost......

Multithread question repost......

Scheduled Pinned Locked Moved C / C++ / MFC
questionalgorithmshelptutorialcareer
3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, I read the MSDN online and get the following sample of using AfxBeginThread: UINT MyThreadProc( LPVOID pParam ) { CMyObject* pObject = (CMyObject*)pParam; if (pObject == NULL || !pObject->IsKindOf(RUNTIME_CLASS(CMyObject))) return 1; // if pObject is not valid // do something with 'pObject' return 0; // thread completed successfully } // inside a different function in the program . . . pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); . . . I understand that.....but : I need to create 50 separate threads (liked 50 different transactions). Since my program is going to simulate the DBMS behavior, in the MyThreadProc() there should be some algorithm to choose whether the current operation of this transaction is READ or WRITE. In this case, all 50 threads will use the same MyThreadProc() and i don't need to make 50 different MyThreadProc(). My question is, 1. Can i create these thread by: for (i=0; i<50; i++) { pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); } 2. How to identify Xth thread after creating 50 threads? 3. What parameter I need to put in order to suspend/resume/kill the 10th (or Xth) Thread? Because let say, one thread A (transaction) is READING a data object, and another thread B wants to WRITE the same data object. In this case, thread B needs to wait until thread A finish. So I have to suspend thread B......and wait....then resume. So, should I call AfxGetThread()???? SuspendThread()??? ResumeThread()???? I need sample codes or examples for each of the above questions. Can anyone help me... please????? Thanks!!!! ;P ;P ;P ;P

    M M 2 Replies Last reply
    0
    • L Lost User

      Hi, I read the MSDN online and get the following sample of using AfxBeginThread: UINT MyThreadProc( LPVOID pParam ) { CMyObject* pObject = (CMyObject*)pParam; if (pObject == NULL || !pObject->IsKindOf(RUNTIME_CLASS(CMyObject))) return 1; // if pObject is not valid // do something with 'pObject' return 0; // thread completed successfully } // inside a different function in the program . . . pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); . . . I understand that.....but : I need to create 50 separate threads (liked 50 different transactions). Since my program is going to simulate the DBMS behavior, in the MyThreadProc() there should be some algorithm to choose whether the current operation of this transaction is READ or WRITE. In this case, all 50 threads will use the same MyThreadProc() and i don't need to make 50 different MyThreadProc(). My question is, 1. Can i create these thread by: for (i=0; i<50; i++) { pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); } 2. How to identify Xth thread after creating 50 threads? 3. What parameter I need to put in order to suspend/resume/kill the 10th (or Xth) Thread? Because let say, one thread A (transaction) is READING a data object, and another thread B wants to WRITE the same data object. In this case, thread B needs to wait until thread A finish. So I have to suspend thread B......and wait....then resume. So, should I call AfxGetThread()???? SuspendThread()??? ResumeThread()???? I need sample codes or examples for each of the above questions. Can anyone help me... please????? Thanks!!!! ;P ;P ;P ;P

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      ChiYung wrote: 1. Can i create these thread by: for (i=0; i<50; i++) The for loop will work fine, although you will need to save the return value from AfxBeginThread(). It returns a pointer to the newly-created CWinThread which you will need later. ChiYung wrote: 2. How to identify Xth thread after creating 50 threads? Keep an array of CWinThread* and fill it with the return values from AfxBeginThread(). As for suspending/resuming, you should use the built-in thread sync objects, instead of suspending threads yourself. Look up critical sections and mutexes in MSDN. --Mike-- Rollin' in my 5.0 With the rag-top down so my hair can blow. My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan and Jamie Salé.

      1 Reply Last reply
      0
      • L Lost User

        Hi, I read the MSDN online and get the following sample of using AfxBeginThread: UINT MyThreadProc( LPVOID pParam ) { CMyObject* pObject = (CMyObject*)pParam; if (pObject == NULL || !pObject->IsKindOf(RUNTIME_CLASS(CMyObject))) return 1; // if pObject is not valid // do something with 'pObject' return 0; // thread completed successfully } // inside a different function in the program . . . pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); . . . I understand that.....but : I need to create 50 separate threads (liked 50 different transactions). Since my program is going to simulate the DBMS behavior, in the MyThreadProc() there should be some algorithm to choose whether the current operation of this transaction is READ or WRITE. In this case, all 50 threads will use the same MyThreadProc() and i don't need to make 50 different MyThreadProc(). My question is, 1. Can i create these thread by: for (i=0; i<50; i++) { pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); } 2. How to identify Xth thread after creating 50 threads? 3. What parameter I need to put in order to suspend/resume/kill the 10th (or Xth) Thread? Because let say, one thread A (transaction) is READING a data object, and another thread B wants to WRITE the same data object. In this case, thread B needs to wait until thread A finish. So I have to suspend thread B......and wait....then resume. So, should I call AfxGetThread()???? SuspendThread()??? ResumeThread()???? I need sample codes or examples for each of the above questions. Can anyone help me... please????? Thanks!!!! ;P ;P ;P ;P

        M Offline
        M Offline
        Morozov Alexey
        wrote on last edited by
        #3

        There are many issuies when you're useing multithreading regarding of your question about simultaneous access to data and whether or not you may call SuspendThread(). You should read at least MSDN topics about CriticalSection usage, about functions WaitForSingleObject/WaitForMultipleObjects and some related topics. Good luck :)

        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