Click here to Skip to main content
15,888,610 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Having different value for a global variable per thread. Pin
sunlin712-Mar-10 1:37
sunlin712-Mar-10 1:37 
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 1:44
Code-o-mat12-Mar-10 1:44 
AnswerRe: Having different value for a global variable per thread. Pin
Jonathan Davies12-Mar-10 1:38
Jonathan Davies12-Mar-10 1:38 
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 1:44
Code-o-mat12-Mar-10 1:44 
AnswerRe: Having different value for a global variable per thread. Pin
Chris Losinger12-Mar-10 3:25
professionalChris Losinger12-Mar-10 3:25 
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 3:30
Code-o-mat12-Mar-10 3:30 
AnswerRe: Having different value for a global variable per thread. Pin
Russell'12-Mar-10 8:48
Russell'12-Mar-10 8:48 
GeneralRe: Having different value for a global variable per thread. Pin
Code-o-mat12-Mar-10 10:12
Code-o-mat12-Mar-10 10:12 
Thanks for the reply. Either i misunderstand you or you misunderstand me but if i am correct you are not talking about what i meant. I will try to explain with an example:
Let's say you have a 3rd party library that performs some task, you can specify a pointer to a function for this 3rd party library and during its processing the library will call your specified function, for example to report progress. Let's say, this method has to have one parameter, an unsigned char that will change from 0 to 100 (percentage) as the 3rd party advances in the task.
void MyProgress(unsigned char percentage)
{
  ...
}

...
SetCallBackProcFor3rdPartyLib(MyProgress); //make the lib call MyProgress to report its progress
Do3rdPartyLibProcessing(); //make the lib do its magic
...
Now, what if you would like to give other parameters to your MyProgress method, for example a handle to a control that should display the progress? You can't just do this:
void MyProgress(unsigned char percentage, HWND control)
{
  ...
}
since the lib requires the method to take only one unsigned char parameter and nothing else. So what you can do to have a way for the MyProgress procedure to have access to this information (the handle) is to create a global variable, store this handle in this before you let the lib do the magic, and use this same variable inside MyProgress.
HWND the_control = NULL;

void MyProgress(unsigned char percentage)
{
  CString str;
  str.Format("%d %%", percentage);
  SetWindowText(the_control, str);
}

...
the_control = handle_of_the_control;
SetCallBackProcFor3rdPartyLib(MyProgress); //make the lib call MyProgress to report its progress
Do3rdPartyLibProcessing(); //make the lib do its magic
...
This works, right? But if you have multiple threads all executing Do3rdPartyLibProcessing and all threads having their very own control to display the progress in, you can no longer use the same approach, since the_control exists only once in memory, so every executing thread would use this same memory when trying to store/retrieve their own control's window handle and bang, welcome to chaosland. So my original question is basicly: how to have a "thread level" global variable (so one that is unique per thread and can be accessed everywhere "inside" the thread). As the others said, TLS (Thread-Local Storage) seems to be just the answer for that.
I hope this clears any misunderstandings, sorry if not.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <

QuestionHow to find source of buffer overflow Pin
WernerP11-Mar-10 23:44
WernerP11-Mar-10 23:44 
AnswerRe: How to find source of buffer overflow Pin
Eugen Podsypalnikov12-Mar-10 0:08
Eugen Podsypalnikov12-Mar-10 0:08 
AnswerRe: How to find source of buffer overflow Pin
Eugen Podsypalnikov12-Mar-10 0:24
Eugen Podsypalnikov12-Mar-10 0:24 
GeneralRe: How to find source of buffer overflow Pin
WernerP12-Mar-10 2:19
WernerP12-Mar-10 2:19 
GeneralRe: How to find source of buffer overflow Pin
Eugen Podsypalnikov12-Mar-10 2:44
Eugen Podsypalnikov12-Mar-10 2:44 
AnswerRe: How to find source of buffer overflow Pin
CPallini12-Mar-10 0:35
mveCPallini12-Mar-10 0:35 
AnswerRe: How to find source of buffer overflow Pin
Adam Roderick J12-Mar-10 0:53
Adam Roderick J12-Mar-10 0:53 
GeneralRe: How to find source of buffer overflow Pin
WernerP12-Mar-10 2:21
WernerP12-Mar-10 2:21 
Questiongiving url to .ini file Pin
jannathali11-Mar-10 23:25
jannathali11-Mar-10 23:25 
AnswerRe: giving url to .ini file Pin
KarstenK12-Mar-10 0:08
mveKarstenK12-Mar-10 0:08 
GeneralRe: giving url to .ini file Pin
jannathali12-Mar-10 1:07
jannathali12-Mar-10 1:07 
AnswerRe: giving url to .ini file Pin
Eugen Podsypalnikov12-Mar-10 2:02
Eugen Podsypalnikov12-Mar-10 2:02 
AnswerRe: giving url to .ini file Pin
David Crow12-Mar-10 3:10
David Crow12-Mar-10 3:10 
QuestionCreate a gui for linux. Pin
krish_kumar11-Mar-10 21:09
krish_kumar11-Mar-10 21:09 
AnswerRe: Create a gui for linux. Pin
KingsGambit11-Mar-10 21:13
KingsGambit11-Mar-10 21:13 
GeneralRe: Create a gui for linux. Pin
krish_kumar11-Mar-10 21:35
krish_kumar11-Mar-10 21:35 
GeneralRe: Create a gui for linux. Pin
KingsGambit11-Mar-10 21:42
KingsGambit11-Mar-10 21:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.