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. how thread work in the os ?

how thread work in the os ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresdebuggingquestion
15 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 Arman S

    From the title I guess you have a multithreading app, isn't it? The errors say nothing valueable unless you describe at which conditions you got them. Here I suspect you have a problem with synchronization issues [particularly, your std::string object gets corrupted by concurrent accesses within the threads]..

    -- ===== Arman

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

    Arman Z. Sahakyan wrote:

    From the title I guess you have a multithreading app, isn't it? The errors say nothing valueable unless you describe at which conditions you got them.

    ya!

    Arman Z. Sahakyan wrote:

    Here I suspect you have a problem with synchronization issues [particularly, your std::string object gets corrupted by concurrent accesses within the threads]..

    yes! when I debug, some time i got some strange values which is actually related with some other thread.

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

    A 1 Reply Last reply
    0
    • A amitmistry_petlad

      Arman Z. Sahakyan wrote:

      From the title I guess you have a multithreading app, isn't it? The errors say nothing valueable unless you describe at which conditions you got them.

      ya!

      Arman Z. Sahakyan wrote:

      Here I suspect you have a problem with synchronization issues [particularly, your std::string object gets corrupted by concurrent accesses within the threads]..

      yes! when I debug, some time i got some strange values which is actually related with some other thread.

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

      A Offline
      A Offline
      Arman S
      wrote on last edited by
      #4

      Are you doing proper syncronization? I know you do not. :) You should prevent multiple accesses to your string data by giving the monopoly to a single thread at a given time period. std classes are not thread safe. This means you should yourself take care of synchronizing their usage. See here[^] about synchronization issues.

      -- ===== Arman

      A 2 Replies Last reply
      0
      • A Arman S

        Are you doing proper syncronization? I know you do not. :) You should prevent multiple accesses to your string data by giving the monopoly to a single thread at a given time period. std classes are not thread safe. This means you should yourself take care of synchronizing their usage. See here[^] about synchronization issues.

        -- ===== Arman

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

        oh please dont :) because my condition is like :((. and after your answer :doh:. but thanks for that this is for u my dear friend :rose: first I will read your code i will understand it properly then i will mail you if I will not get the result ok thanks once again !

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

        R 1 Reply Last reply
        0
        • A amitmistry_petlad

          oh please dont :) because my condition is like :((. and after your answer :doh:. but thanks for that this is for u my dear friend :rose: first I will read your code i will understand it properly then i will mail you if I will not get the result ok thanks once again !

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

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #6

          This is in response to your email to me. I have not worked too much on threads, but will surely help you if I could. My suggestion is that you post it at CP, and you will get plenty of us. Moreover, threads are not something that I've mastered. :)

          1 Reply Last reply
          0
          • A Arman S

            Are you doing proper syncronization? I know you do not. :) You should prevent multiple accesses to your string data by giving the monopoly to a single thread at a given time period. std classes are not thread safe. This means you should yourself take care of synchronizing their usage. See here[^] about synchronization issues.

            -- ===== Arman

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

            I have tried out my way but not getting the proper result. string problem is as remain as but even dw never goes in the any of the cas like case WAIT_OBJECT_0 + 1: +2 or +3. what should I do ? how can i manage it ?

            unsigned __stdcall Thread(void* pArguments )
            {
            	HRESULT hr=NULL;
            	struct argument_list *Lparam= (argument_list *)pArguments ;
            	Package pkg;
            	
            	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 );    	
            	}
            

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

            A 1 Reply Last reply
            0
            • A amitmistry_petlad

              I have tried out my way but not getting the proper result. string problem is as remain as but even dw never goes in the any of the cas like case WAIT_OBJECT_0 + 1: +2 or +3. what should I do ? how can i manage it ?

              unsigned __stdcall Thread(void* pArguments )
              {
              	HRESULT hr=NULL;
              	struct argument_list *Lparam= (argument_list *)pArguments ;
              	Package pkg;
              	
              	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 );    	
              	}
              

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

              A Offline
              A Offline
              Arman S
              wrote on last edited by
              #8

              Supposedly the case WAIT_FAILED works. Anyway ... A couple of questions; DWORD dw=WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,5000 ); What you want to do with this code? Where does LitBoxItemCount come from? Where does hThread come from? How many threads do you have? I need to know the answers of these questions before I can say the reasons of your problem.

              -- ===== Arman

              A 2 Replies Last reply
              0
              • A Arman S

                Supposedly the case WAIT_FAILED works. Anyway ... A couple of questions; DWORD dw=WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,5000 ); What you want to do with this code? Where does LitBoxItemCount come from? Where does hThread come from? How many threads do you have? I need to know the answers of these questions before I can say the reasons of your problem.

                -- ===== Arman

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

                May I send you detail on your email? because the proper format and written mail is ready. May I ...?

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

                A 1 Reply Last reply
                0
                • A amitmistry_petlad

                  May I send you detail on your email? because the proper format and written mail is ready. May I ...?

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

                  A Offline
                  A Offline
                  Arman S
                  wrote on last edited by
                  #10

                  Ok, do it.

                  -- ===== Arman

                  A 1 Reply Last reply
                  0
                  • A Arman S

                    Ok, do it.

                    -- ===== Arman

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

                    have you got any idea or do you need any feed back?

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

                    A 1 Reply Last reply
                    0
                    • A amitmistry_petlad

                      have you got any idea or do you need any feed back?

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

                      A Offline
                      A Offline
                      Arman S
                      wrote on last edited by
                      #12

                      This is a multithreading issue and to overcome one needs a proper level of detailed information. What you have posted as code didn't cover all the details and thus you should provide more details. I've already asked about the questions that are not clear [at least to me].

                      -- ===== Arman

                      A 2 Replies Last reply
                      0
                      • A Arman S

                        Supposedly the case WAIT_FAILED works. Anyway ... A couple of questions; DWORD dw=WaitForMultipleObjects(ListBoxItemCount,hThread,TRUE,5000 ); What you want to do with this code? Where does LitBoxItemCount come from? Where does hThread come from? How many threads do you have? I need to know the answers of these questions before I can say the reasons of your problem.

                        -- ===== Arman

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

                        Arman Z. Sahakyan wrote:

                        What you want to do with this code? Where does LitBoxItemCount come from? Where does hThread come from? How many threads do you have?

                        What you want to do with this code?

                        with this code i am tring to synchronize my threads.

                        Where does LitBoxItemCount come from?

                        this will come from the listview control (itemcount).I put in my code as global so Global function Thread can use it.

                        Where does hThread come from?

                        hThread is array of handle which assigne the thread.

                        How many threads do you have?

                        well ,for sample application have created the three worker thread (means if there are total three files in the listview control so itemcount will be three means LitBoxItemCount)

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

                        1 Reply Last reply
                        0
                        • A Arman S

                          This is a multithreading issue and to overcome one needs a proper level of detailed information. What you have posted as code didn't cover all the details and thus you should provide more details. I've already asked about the questions that are not clear [at least to me].

                          -- ===== Arman

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

                          would you need any thing more specific information? I am ready to provide you.

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

                          1 Reply Last reply
                          0
                          • A Arman S

                            This is a multithreading issue and to overcome one needs a proper level of detailed information. What you have posted as code didn't cover all the details and thus you should provide more details. I've already asked about the questions that are not clear [at least to me].

                            -- ===== Arman

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

                            just one question. I need your little help. when and how the thread manage? Suppose I have four thread hThread[0] = (HANDLE)_beginthreadex(NULL, 0, &Thread ,&l,NULL, &threadID); hThread[1] = (HANDLE)_beginthreadex(NULL, 0, &Thread ,&l,NULL, &threadID); hThread[2] = (HANDLE)_beginthreadex(NULL, 0, &Thread ,&l,NULL, &threadID); hThread[3] = (HANDLE)_beginthreadex(NULL, 0, &Thread ,&l,NULL, &threadID); and the global function

                            unsigned __stdcall Thread(void* pArguments )
                            {
                            	HRESULT hr=NULL;
                            	struct argument_list *Lparam= (argument_list *)pArguments ;
                            	Package pkg;
                            	threadID++;
                            	
                            	DWORD  dw=WaitForMultipleObjects(ListBoxItemCount,hThread[threadID],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 can i decide the when which thread come at hThread[],

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

                            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