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. Thread parameter use same value for all thread ?????

Thread parameter use same value for all thread ?????

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 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 amitmistry_petlad

    hi ! dear all! well now all threads are working and running but then it use the same paramters for all the thread. I confuse :doh: here why :confused: ??? . I need somebody's urgent help. Thanks in advance

    "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
    #2

    Could you clarify your question? What do you mean by saying but then it use the same paramters for all the thread ?

    -- ===== Arman

    A 1 Reply Last reply
    0
    • A Arman S

      Could you clarify your question? What do you mean by saying but then it use the same paramters for all the thread ?

      -- ===== Arman

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

      well I say you heartly. I have been waiting for your answer. if i will write the post with your name then it is not good but i think what i need is what i get.(i mean answer from arman). ok, as you know previously about my code the loop is runing and thread created and i have made the array of threads. right. But now all thread running but the structure variable(structur pointer actually which send the parameter to the Thread function for further process using the same values for all created thread.

      e.g

      for(int i=0;i<2;i++)
      {

      struct argument_list *l;//[MAX_PATH];
      l=(argument_list *)malloc(sizeof(argument_list));

      					 l->pszInFile=bufferwithpath; 
      					 l->pszOutFil=w\_Output;
      					 l->\_ProtectSet=\_ProtectSet;
      					 l->hList=hList;
      
      					
      					 strcpy(l->Host,HOST);
      					 l->hwndEncrypt=hwndEncrypt;
      					 l->hWndinoutfiledir=hWndinoutfiledir;
      					 l->hwndParent=hDlg;		
      
      					 strcpy(l->InitPackageRequest,sINIT\_PACKAGE\_RESPONSE.c\_str());						 
      					 l->Port=PORT;
      
      					 strcpy(l->ScriptFile,COMMUNICATOR\_SCRIPT\_FILE\_PATH);
      
      					 strcpy(l->UserID,UserID.c\_str());
      
      
      					 hThread\[i\]=(HANDLE)\_beginthread(&Thread ,0 ,**`l`**);					 
      					 //WaitForSingleObject(hThread,INFINITE);
      				 }			
      				WaitForMultipleObjects(2, hThread, FALSE, INFINITE);
      

      //The value of l remains same for all the created. means ,The values show correct when the loop is going on. But when thread call the Thread function below in that case. 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); all parameter values shown same for all threads .why??? it must be different according to the thread created with. suppose for thread one l could be Lparam->pszInFile ="a.wmv" Lparam->pszOutFil="DRMa.wmv" .... ..... ..... ..... for thread 2 Lparam->pszInFile ="b.wmv" Lparam->pszOutFil="DRMb.wmv" .... ..... ..... ..... i need this way for consucative thread calls ======== but it shown every time first thread's values for all other thread. values l for first thread. Lparam->pszInFile ="a.wmv" Lparam->pszOutFil="DRMa.wmv" .... ..... ..... ..... I think now you might got is it ? ;) :)

      void Thread(void* pArguments )

      A 2 Replies Last reply
      0
      • A amitmistry_petlad

        well I say you heartly. I have been waiting for your answer. if i will write the post with your name then it is not good but i think what i need is what i get.(i mean answer from arman). ok, as you know previously about my code the loop is runing and thread created and i have made the array of threads. right. But now all thread running but the structure variable(structur pointer actually which send the parameter to the Thread function for further process using the same values for all created thread.

        e.g

        for(int i=0;i<2;i++)
        {

        struct argument_list *l;//[MAX_PATH];
        l=(argument_list *)malloc(sizeof(argument_list));

        					 l->pszInFile=bufferwithpath; 
        					 l->pszOutFil=w\_Output;
        					 l->\_ProtectSet=\_ProtectSet;
        					 l->hList=hList;
        
        					
        					 strcpy(l->Host,HOST);
        					 l->hwndEncrypt=hwndEncrypt;
        					 l->hWndinoutfiledir=hWndinoutfiledir;
        					 l->hwndParent=hDlg;		
        
        					 strcpy(l->InitPackageRequest,sINIT\_PACKAGE\_RESPONSE.c\_str());						 
        					 l->Port=PORT;
        
        					 strcpy(l->ScriptFile,COMMUNICATOR\_SCRIPT\_FILE\_PATH);
        
        					 strcpy(l->UserID,UserID.c\_str());
        
        
        					 hThread\[i\]=(HANDLE)\_beginthread(&Thread ,0 ,**`l`**);					 
        					 //WaitForSingleObject(hThread,INFINITE);
        				 }			
        				WaitForMultipleObjects(2, hThread, FALSE, INFINITE);
        

        //The value of l remains same for all the created. means ,The values show correct when the loop is going on. But when thread call the Thread function below in that case. 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); all parameter values shown same for all threads .why??? it must be different according to the thread created with. suppose for thread one l could be Lparam->pszInFile ="a.wmv" Lparam->pszOutFil="DRMa.wmv" .... ..... ..... ..... for thread 2 Lparam->pszInFile ="b.wmv" Lparam->pszOutFil="DRMb.wmv" .... ..... ..... ..... i need this way for consucative thread calls ======== but it shown every time first thread's values for all other thread. values l for first thread. Lparam->pszInFile ="a.wmv" Lparam->pszOutFil="DRMa.wmv" .... ..... ..... ..... I think now you might got is it ? ;) :)

        void Thread(void* pArguments )

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

        I think now you might got is it ? Ok, wait a little, I will reply soon.

        -- ===== Arman

        1 Reply Last reply
        0
        • A amitmistry_petlad

          well I say you heartly. I have been waiting for your answer. if i will write the post with your name then it is not good but i think what i need is what i get.(i mean answer from arman). ok, as you know previously about my code the loop is runing and thread created and i have made the array of threads. right. But now all thread running but the structure variable(structur pointer actually which send the parameter to the Thread function for further process using the same values for all created thread.

          e.g

          for(int i=0;i<2;i++)
          {

          struct argument_list *l;//[MAX_PATH];
          l=(argument_list *)malloc(sizeof(argument_list));

          					 l->pszInFile=bufferwithpath; 
          					 l->pszOutFil=w\_Output;
          					 l->\_ProtectSet=\_ProtectSet;
          					 l->hList=hList;
          
          					
          					 strcpy(l->Host,HOST);
          					 l->hwndEncrypt=hwndEncrypt;
          					 l->hWndinoutfiledir=hWndinoutfiledir;
          					 l->hwndParent=hDlg;		
          
          					 strcpy(l->InitPackageRequest,sINIT\_PACKAGE\_RESPONSE.c\_str());						 
          					 l->Port=PORT;
          
          					 strcpy(l->ScriptFile,COMMUNICATOR\_SCRIPT\_FILE\_PATH);
          
          					 strcpy(l->UserID,UserID.c\_str());
          
          
          					 hThread\[i\]=(HANDLE)\_beginthread(&Thread ,0 ,**`l`**);					 
          					 //WaitForSingleObject(hThread,INFINITE);
          				 }			
          				WaitForMultipleObjects(2, hThread, FALSE, INFINITE);
          

          //The value of l remains same for all the created. means ,The values show correct when the loop is going on. But when thread call the Thread function below in that case. 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); all parameter values shown same for all threads .why??? it must be different according to the thread created with. suppose for thread one l could be Lparam->pszInFile ="a.wmv" Lparam->pszOutFil="DRMa.wmv" .... ..... ..... ..... for thread 2 Lparam->pszInFile ="b.wmv" Lparam->pszOutFil="DRMb.wmv" .... ..... ..... ..... i need this way for consucative thread calls ======== but it shown every time first thread's values for all other thread. values l for first thread. Lparam->pszInFile ="a.wmv" Lparam->pszOutFil="DRMa.wmv" .... ..... ..... ..... I think now you might got is it ? ;) :)

          void Thread(void* pArguments )

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

          Well. Your code has lots of things to recorrect. Though the abnormal behavior you described I could hardly connect to them. Let's start from general issues. 1) Are you using C and not C++? 2) Never use _beginthread! Use _beginthreadex instead. The first one is problematic and may cause hard-to-recognize errors! Your call should be; unsigned int tid; // thread id hHandle[j] = (HANDLE ) _beginthreadex(NULL, 0, &Thread, l, 0, &tid); 3) Never use _endthreadex (nor use _endthread). These functions are mainly for urgent termination situations which are not inherent [normal] in a well designed architecture. Instead do 'return 0;'. 4) You should change the declaration form of Thread function; unsigned int should be returned to indicate the exit code. Now it should be; UINT Thread(void *pArgs) 5) Are you sure your values are different inside the thread creation 'for' loop? Anyway, going on.. 6) If your in C++, use new rather than malloc [but I thing you are in C]. 7) Strings are erroneous! Remember that in a multithreaded application YOU SHOULD BE VERY VERY CAREFUL WHEN PASSING STRINGS INTO DIFFERENT THREADS! So your code is wrong. YOU SHOULD CREATE SEPARATE STRING BUFFERS PER THREAD. That is;

          for(int i=0;i<2;i++)
          {
          struct argument_list *l = (argument_list *) malloc(sizeof(argument_list));
          ...

          l->Host = (LPTSTR)malloc(MAX_PATH); // this kind of allocations should be for all strings of argument_list
          strcpy(l->Host, HOST);
          ...

          1. To have a solid test inside Thread function also pass the j (loop index) to them; I mean add an extra int field to argument_list and observe its value from each thread function. They all should be different. 9) And one thing about Thread again. Never do such things; _//WaitForSingleObject(GetCurrentThread(),5000);_ This makes no sense. Finally, see what you can get from these notes. Take into consideretion each of them. Then I think you will overcome your problem. You could tell me when you do smth. :)

          -- ===== Arman

          A 1 Reply Last reply
          0
          • A Arman S

            Well. Your code has lots of things to recorrect. Though the abnormal behavior you described I could hardly connect to them. Let's start from general issues. 1) Are you using C and not C++? 2) Never use _beginthread! Use _beginthreadex instead. The first one is problematic and may cause hard-to-recognize errors! Your call should be; unsigned int tid; // thread id hHandle[j] = (HANDLE ) _beginthreadex(NULL, 0, &Thread, l, 0, &tid); 3) Never use _endthreadex (nor use _endthread). These functions are mainly for urgent termination situations which are not inherent [normal] in a well designed architecture. Instead do 'return 0;'. 4) You should change the declaration form of Thread function; unsigned int should be returned to indicate the exit code. Now it should be; UINT Thread(void *pArgs) 5) Are you sure your values are different inside the thread creation 'for' loop? Anyway, going on.. 6) If your in C++, use new rather than malloc [but I thing you are in C]. 7) Strings are erroneous! Remember that in a multithreaded application YOU SHOULD BE VERY VERY CAREFUL WHEN PASSING STRINGS INTO DIFFERENT THREADS! So your code is wrong. YOU SHOULD CREATE SEPARATE STRING BUFFERS PER THREAD. That is;

            for(int i=0;i<2;i++)
            {
            struct argument_list *l = (argument_list *) malloc(sizeof(argument_list));
            ...

            l->Host = (LPTSTR)malloc(MAX_PATH); // this kind of allocations should be for all strings of argument_list
            strcpy(l->Host, HOST);
            ...

            1. To have a solid test inside Thread function also pass the j (loop index) to them; I mean add an extra int field to argument_list and observe its value from each thread function. They all should be different. 9) And one thing about Thread again. Never do such things; _//WaitForSingleObject(GetCurrentThread(),5000);_ This makes no sense. Finally, see what you can get from these notes. Take into consideretion each of them. Then I think you will overcome your problem. You could tell me when you do smth. :)

            -- ===== Arman

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

            Thank you very much for your nice guideline.:rose: and please Look at the structure once.

            struct argument_list
            {
            	WCHAR *pszInFile; //I was talking about this  
            	WCHAR *pszOutFil; //and  this one 
            	HWND  hwndParent;
            	
            	char Host[MAX_PATH];
            	char UserID[MAX_PATH];
            	char InitPackageRequest[500000];
            	char ScriptFile[MAX_PATH];
            
            	INTERNET_PORT Port;
            	HWND hList;
            	TagEditor *_ProtectSet;
            	HWND hWndinoutfiledir;
            	HWND hwndEncrypt;
            };
            

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

            A 1 Reply Last reply
            0
            • A amitmistry_petlad

              Thank you very much for your nice guideline.:rose: and please Look at the structure once.

              struct argument_list
              {
              	WCHAR *pszInFile; //I was talking about this  
              	WCHAR *pszOutFil; //and  this one 
              	HWND  hwndParent;
              	
              	char Host[MAX_PATH];
              	char UserID[MAX_PATH];
              	char InitPackageRequest[500000];
              	char ScriptFile[MAX_PATH];
              
              	INTERNET_PORT Port;
              	HWND hList;
              	TagEditor *_ProtectSet;
              	HWND hWndinoutfiledir;
              	HWND hwndEncrypt;
              };
              

              "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
              #7

              Oho, this is important. I do not know why you are using UNICODE strings though [I'm not saying this is bad :)]. Try the following inside the 'for' loop instead of these two lines; // l->pszInFile = bufferwithpath; // l->pszOutFil = w_Output;

              l->pszInFile = (LPWSTR) malloc( 2 * wcslen(bufferwithpath) );
              wcscpy(l->pszInFile, bufferwithpath);

              l->pszOutFile = (LPWSTR) malloc( 2 * wcslen(w_Output) );
              wcscpy(l->pszInFile, bufferwithpath);

              -- ===== Arman

              A A 2 Replies Last reply
              0
              • A Arman S

                Oho, this is important. I do not know why you are using UNICODE strings though [I'm not saying this is bad :)]. Try the following inside the 'for' loop instead of these two lines; // l->pszInFile = bufferwithpath; // l->pszOutFil = w_Output;

                l->pszInFile = (LPWSTR) malloc( 2 * wcslen(bufferwithpath) );
                wcscpy(l->pszInFile, bufferwithpath);

                l->pszOutFile = (LPWSTR) malloc( 2 * wcslen(w_Output) );
                wcscpy(l->pszInFile, bufferwithpath);

                -- ===== Arman

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

                l->pszInFile = (LPWSTR) malloc( 2 * wcslen(bufferwithpath) );
                wcscpy(l->pszInFile, bufferwithpath);
                l->pszOutFile = (LPWSTR) malloc( 2 * wcslen(w_Output) );
                wcscpy(l->pszOutFile , w_Output);

                -- ===== Arman

                A 1 Reply Last reply
                0
                • A Arman S

                  l->pszInFile = (LPWSTR) malloc( 2 * wcslen(bufferwithpath) );
                  wcscpy(l->pszInFile, bufferwithpath);
                  l->pszOutFile = (LPWSTR) malloc( 2 * wcslen(w_Output) );
                  wcscpy(l->pszOutFile , w_Output);

                  -- ===== Arman

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

                  Dear arman, This is for you.:rose: :rose: :rose: Thank you very much but I asked one question. May I asked you? Amit

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

                  1 Reply Last reply
                  0
                  • A Arman S

                    Oho, this is important. I do not know why you are using UNICODE strings though [I'm not saying this is bad :)]. Try the following inside the 'for' loop instead of these two lines; // l->pszInFile = bufferwithpath; // l->pszOutFil = w_Output;

                    l->pszInFile = (LPWSTR) malloc( 2 * wcslen(bufferwithpath) );
                    wcscpy(l->pszInFile, bufferwithpath);

                    l->pszOutFile = (LPWSTR) malloc( 2 * wcslen(w_Output) );
                    wcscpy(l->pszInFile, bufferwithpath);

                    -- ===== Arman

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

                    Arman Z. Sahakyan wrote:

                    // l->pszInFile = bufferwithpath; // l->pszOutFil = w_Output; l->pszInFile = (LPWSTR) malloc( 2 * wcslen(bufferwithpath) );wcscpy(l->pszInFile, bufferwithpath);l->pszOutFile = (LPWSTR) malloc( 2 * wcslen(w_Output) );wcscpy(l->pszInFile, bufferwithpath);

                    HI ! arman when can we need to allocate the memory to the variable? In the above case I have just try to assign that i know its wrong but , the basic question is when can we should allocate memory? I know that array already occupay memory at compile time , and I also konw that we can allocate memory runtime by malloc/calloc or new in c++, but where and in which condition we have to use it. please regret me about my spells and english. can you kindly explain me?

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

                    1 Reply Last reply
                    0
                    • A amitmistry_petlad

                      hi ! dear all! well now all threads are working and running but then it use the same paramters for all the thread. I confuse :doh: here why :confused: ??? . I need somebody's urgent help. Thanks in advance

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

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #11

                      amitmistry_petlad wrote:

                      well now all threads are working and running but then it use the same paramters for all the thread. I confuse :doh: here why :confused: ???

                      thats just LPVOID.. you can typecast anything into it!

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                      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