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. Need little help in Worker threads [modified]

Need little help in Worker threads [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebugginghelp
6 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.
  • A Offline
    A Offline
    amitmistry_petlad
    wrote on last edited by
    #1

    Hi! In my application I am using multithreading. for that I have used the loop. HANDLE hThread[3]; for(int i=0;i<3;i++) { hThread[i]=(HANDLE)_beginthreadex(NULL, 0, &Thread ,l,NULL, &threadID); } unsigned __stdcall Thread(void* pArguments ) { HRESULT hr=NULL; struct argument_list *Lparam= (argument_list *)pArguments ; Package pkg; threadID++; DWORD dw=WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,5000 ); switch (dw) { case WAIT_OBJECT_0 + 0: // a process with h[0] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_OBJECT_0 + 1: // a process with h[1] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_OBJECT_0 + 2: // a process with h[2] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_FAILED: // failure break; case WAIT_TIMEOUT: // no processes exited during 5000ms break; } //printf( "In second thread...\n" ); if(hr==S_OK) { MessageBox(0,L"thread gone finished" ,L"therad",0); _endthreadex( 0 ); } threadID--; return 0; } how these worker thread works and how they will run. means ,how can i identified that which thread work at hThread[threadID/*it is Relevant with i */ ], 1)I have to wait/stop the main thread until the work threads finished his task. 2)when can all thread start. or will it start immidate at the loop time or first it will create all. then it start ? becuse in my application when i debug some started with loop and some afterwards. -- modified at 7:09 Friday 4th May, 2007

    "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-In

    C R 2 Replies Last reply
    0
    • A amitmistry_petlad

      Hi! In my application I am using multithreading. for that I have used the loop. HANDLE hThread[3]; for(int i=0;i<3;i++) { hThread[i]=(HANDLE)_beginthreadex(NULL, 0, &Thread ,l,NULL, &threadID); } unsigned __stdcall Thread(void* pArguments ) { HRESULT hr=NULL; struct argument_list *Lparam= (argument_list *)pArguments ; Package pkg; threadID++; DWORD dw=WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,5000 ); switch (dw) { case WAIT_OBJECT_0 + 0: // a process with h[0] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_OBJECT_0 + 1: // a process with h[1] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_OBJECT_0 + 2: // a process with h[2] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_FAILED: // failure break; case WAIT_TIMEOUT: // no processes exited during 5000ms break; } //printf( "In second thread...\n" ); if(hr==S_OK) { MessageBox(0,L"thread gone finished" ,L"therad",0); _endthreadex( 0 ); } threadID--; return 0; } how these worker thread works and how they will run. means ,how can i identified that which thread work at hThread[threadID/*it is Relevant with i */ ], 1)I have to wait/stop the main thread until the work threads finished his task. 2)when can all thread start. or will it start immidate at the loop time or first it will create all. then it start ? becuse in my application when i debug some started with loop and some afterwards. -- modified at 7:09 Friday 4th May, 2007

      "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-In

      C Offline
      C Offline
      Chilli71
      wrote on last edited by
      #2

      Hi! The threads are all created in the loop. However they are created in a suspended state. The scheduler of the operating sytem will activate them (put them in running state) according to their priority and overall system load. Usually this will be in the order you create the threads. In prinzipal using a static (global) variable as a thread counter is a good idea. But I strongly recommend to put the access to this variable into a critical section just for the case that the read and write operation (and whatever you will add in future) is not atomic. Since the worker threads are identical clones it does not matter if the creation sequence would deviate from the sequence the threads start. However, if you want to know the creation sequence in the thread, you can just pass your loop index i as parameter. kind regards Leo

      A 1 Reply Last reply
      0
      • C Chilli71

        Hi! The threads are all created in the loop. However they are created in a suspended state. The scheduler of the operating sytem will activate them (put them in running state) according to their priority and overall system load. Usually this will be in the order you create the threads. In prinzipal using a static (global) variable as a thread counter is a good idea. But I strongly recommend to put the access to this variable into a critical section just for the case that the read and write operation (and whatever you will add in future) is not atomic. Since the worker threads are identical clones it does not matter if the creation sequence would deviate from the sequence the threads start. However, if you want to know the creation sequence in the thread, you can just pass your loop index i as parameter. kind regards Leo

        A Offline
        A Offline
        amitmistry_petlad
        wrote on last edited by
        #3

        thanks ! but now i am in big confusion here.i have used array of handle right! now runtime which handle will comes at which stage how can i know that? becuse hThread[0]=.... hThread[1]=.... hThread[2]=.... hThread[3]=.... and at **WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,0);** we write only the base hThread address the how the thread identified. by system. hThread[i], i think it was my wrong idea , because after then it gives me the constant handle error , anyways I just want to know why the above method fail.

        "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

        C 1 Reply Last reply
        0
        • A amitmistry_petlad

          Hi! In my application I am using multithreading. for that I have used the loop. HANDLE hThread[3]; for(int i=0;i<3;i++) { hThread[i]=(HANDLE)_beginthreadex(NULL, 0, &Thread ,l,NULL, &threadID); } unsigned __stdcall Thread(void* pArguments ) { HRESULT hr=NULL; struct argument_list *Lparam= (argument_list *)pArguments ; Package pkg; threadID++; DWORD dw=WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,5000 ); switch (dw) { case WAIT_OBJECT_0 + 0: // a process with h[0] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_OBJECT_0 + 1: // a process with h[1] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_OBJECT_0 + 2: // a process with h[2] descriptor has exited hr=pkg.EncodeMediaContent(Lparam->pszInFile,Lparam->pszOutFil,Lparam->hwndParent,Lparam->Host,Lparam->UserID,Lparam->InitPackageRequest,Lparam->ScriptFile,Lparam->Port,Lparam->hList,Lparam->_ProtectSet,Lparam->hWndinoutfiledir,Lparam->hwndEncrypt); break; case WAIT_FAILED: // failure break; case WAIT_TIMEOUT: // no processes exited during 5000ms break; } //printf( "In second thread...\n" ); if(hr==S_OK) { MessageBox(0,L"thread gone finished" ,L"therad",0); _endthreadex( 0 ); } threadID--; return 0; } how these worker thread works and how they will run. means ,how can i identified that which thread work at hThread[threadID/*it is Relevant with i */ ], 1)I have to wait/stop the main thread until the work threads finished his task. 2)when can all thread start. or will it start immidate at the loop time or first it will create all. then it start ? becuse in my application when i debug some started with loop and some afterwards. -- modified at 7:09 Friday 4th May, 2007

          "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-In

          R Offline
          R Offline
          Roger Stoltz
          wrote on last edited by
          #4

          Amit, please do not email me or anyone else directly asking for help. I saw you've previously done so at least to brahmma earlier and twice to me today.

          Chris Maunder wrote:

          Everyone here helps because they enjoy helping others, not because it's their job.

          in this[^] valuable post. Regarding your threading problem, I have the following opinion:

          amitmistry_petlad wrote:

          1)I have to wait/stop the main thread until the work threads finished his task.

          This implies that you've misunderstood the concept of multithreading since you're still doing things in serial. Spawning other threads is for doing things in parallel. You have to re-evaluate your design. I also suggest you read Joe Newcomer's article about worker threads here[^] before asking more questions. The article will help you understand the basics, asking the "right" questions in this forum and the answers you get will also make more sense to you. The article refers to how to use MFC classes, but the concept of multithreading is still the same. You don't seem to be using MFC, but you haven't stated that clearly in any of your posts so I'm not really sure whether you use MFC or not. I also advise you to read the other articles Joe refers to in his article about multithreading to get a more complete picture of the concept. In my opinion, Newcomer's article explains the basics of multithreading better than any article I've found. I really hope this helps you. :rose:


          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          A 1 Reply Last reply
          0
          • R Roger Stoltz

            Amit, please do not email me or anyone else directly asking for help. I saw you've previously done so at least to brahmma earlier and twice to me today.

            Chris Maunder wrote:

            Everyone here helps because they enjoy helping others, not because it's their job.

            in this[^] valuable post. Regarding your threading problem, I have the following opinion:

            amitmistry_petlad wrote:

            1)I have to wait/stop the main thread until the work threads finished his task.

            This implies that you've misunderstood the concept of multithreading since you're still doing things in serial. Spawning other threads is for doing things in parallel. You have to re-evaluate your design. I also suggest you read Joe Newcomer's article about worker threads here[^] before asking more questions. The article will help you understand the basics, asking the "right" questions in this forum and the answers you get will also make more sense to you. The article refers to how to use MFC classes, but the concept of multithreading is still the same. You don't seem to be using MFC, but you haven't stated that clearly in any of your posts so I'm not really sure whether you use MFC or not. I also advise you to read the other articles Joe refers to in his article about multithreading to get a more complete picture of the concept. In my opinion, Newcomer's article explains the basics of multithreading better than any article I've found. I really hope this helps you. :rose:


            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
            "High speed never compensates for wrong direction!" - unknown

            A Offline
            A Offline
            amitmistry_petlad
            wrote on last edited by
            #5

            Extremely Sorry ! :sigh: In future, I will take care .

            "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

            1 Reply Last reply
            0
            • A amitmistry_petlad

              thanks ! but now i am in big confusion here.i have used array of handle right! now runtime which handle will comes at which stage how can i know that? becuse hThread[0]=.... hThread[1]=.... hThread[2]=.... hThread[3]=.... and at **WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,0);** we write only the base hThread address the how the thread identified. by system. hThread[i], i think it was my wrong idea , because after then it gives me the constant handle error , anyways I just want to know why the above method fail.

              "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

              C Offline
              C Offline
              Chilli71
              wrote on last edited by
              #6

              OK, I'm no specialist for WaitObject. And I still do not understand what the code is intended for. But lets start from the beginning. You are creating 3 threads (thread 0 to thread 2) While you have hardcoded the three in the loop index you use the variable ListBoxItemCount in the thread. Looking at your previous answer you say hThread[0] ... hThread[3]. How much is ListBoxItemCount? Because hThread[3] is never initialized. In the way the code is now, it could happen that a task switch occurs in the thread starting loop, after one or two threads where created. In this case the Wait function must fail because not all thread handles in the list are initialized. I understand that this is only a sample code to demonstrate your problem. But I have also doubts that you are going in the right direction. It is very likely, that if you change your design, the code that troubles you will disappear. Greets Leo

              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