Thread parameter use same value for all thread ?????
-
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
-
Could you clarify your question? What do you mean by saying but then it use the same paramters for all the thread ?
-- ===== Arman
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 )
-
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 )
-
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 )
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);
...- 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
- 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;
-
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);
...- 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
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
- 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;
-
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
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
-
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
-
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
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
-
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
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
-
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
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