static member functions & multithreading
-
Hi all, here´s a "tricky" question. let´s say i plan to use serveral threads on my app, so i write a class to wrap threads, its header could look something like this (in pseudo-code): class CThread { public: BOOL Start(); // called to create the thread void End(); // called to terminate the thread static DWORD WINAPI MyThreadProc(LPVOID pData); // the threadproc }; and in my app i could create several of these at any given time, like: void WhatEver() { CThread threads[5]; for(int i=0; i<5; i++) threads[i].Start(); } to my understanding, when a class has a static member function, one and ONLY ONE "instance" of that function´s code exsists in my application as soon as the app starts running, even if there are several instances of that class present, they all "share" the same Function code (right?) so, there are (in the above code) 5 instances of the CThread class, but there is ONLY one instance of the MyThreadProc() function, so here´s what i dont understand, ¿what if the threads do some time consuming operation?, ¿how can threads[1] start "running" if threads[0] has´nt finished yet, and thread[2] do its stuff while thread[1] is busy and so on? or to put it in other words, if theres only one instance of MyThreadProc(), ¿how can it be used at the same time by all threads? Thanks for reading!
-
Hi all, here´s a "tricky" question. let´s say i plan to use serveral threads on my app, so i write a class to wrap threads, its header could look something like this (in pseudo-code): class CThread { public: BOOL Start(); // called to create the thread void End(); // called to terminate the thread static DWORD WINAPI MyThreadProc(LPVOID pData); // the threadproc }; and in my app i could create several of these at any given time, like: void WhatEver() { CThread threads[5]; for(int i=0; i<5; i++) threads[i].Start(); } to my understanding, when a class has a static member function, one and ONLY ONE "instance" of that function´s code exsists in my application as soon as the app starts running, even if there are several instances of that class present, they all "share" the same Function code (right?) so, there are (in the above code) 5 instances of the CThread class, but there is ONLY one instance of the MyThreadProc() function, so here´s what i dont understand, ¿what if the threads do some time consuming operation?, ¿how can threads[1] start "running" if threads[0] has´nt finished yet, and thread[2] do its stuff while thread[1] is busy and so on? or to put it in other words, if theres only one instance of MyThreadProc(), ¿how can it be used at the same time by all threads? Thanks for reading!
static
means different things when applied to a member function versus a member variable.static
on a method means that the method does not receive athis
pointer. There's no concept of "instances" for methods - the compiler generates the code for the method, and threads run that code whenever you call the method. So you can have many threads calling the same method with no problems. (Of course, you have to protect any data from simultaneous access if necessary, but that's another topic altogether.) --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer. -- Michael P. Butler in the Lounge -
Hi all, here´s a "tricky" question. let´s say i plan to use serveral threads on my app, so i write a class to wrap threads, its header could look something like this (in pseudo-code): class CThread { public: BOOL Start(); // called to create the thread void End(); // called to terminate the thread static DWORD WINAPI MyThreadProc(LPVOID pData); // the threadproc }; and in my app i could create several of these at any given time, like: void WhatEver() { CThread threads[5]; for(int i=0; i<5; i++) threads[i].Start(); } to my understanding, when a class has a static member function, one and ONLY ONE "instance" of that function´s code exsists in my application as soon as the app starts running, even if there are several instances of that class present, they all "share" the same Function code (right?) so, there are (in the above code) 5 instances of the CThread class, but there is ONLY one instance of the MyThreadProc() function, so here´s what i dont understand, ¿what if the threads do some time consuming operation?, ¿how can threads[1] start "running" if threads[0] has´nt finished yet, and thread[2] do its stuff while thread[1] is busy and so on? or to put it in other words, if theres only one instance of MyThreadProc(), ¿how can it be used at the same time by all threads? Thanks for reading!
I think your confusing class instances with threads. You are correct, when you define a class function as static, there is only one copy of the function(per thread). But a thread has it's own stack. So each thread executes it's own copy of the static function. So any local variables inside the static function are multi-thread safe. But any global/static variables are not thread safe. They are shared by all threads threads.
-
I think your confusing class instances with threads. You are correct, when you define a class function as static, there is only one copy of the function(per thread). But a thread has it's own stack. So each thread executes it's own copy of the static function. So any local variables inside the static function are multi-thread safe. But any global/static variables are not thread safe. They are shared by all threads threads.
-
static
means different things when applied to a member function versus a member variable.static
on a method means that the method does not receive athis
pointer. There's no concept of "instances" for methods - the compiler generates the code for the method, and threads run that code whenever you call the method. So you can have many threads calling the same method with no problems. (Of course, you have to protect any data from simultaneous access if necessary, but that's another topic altogether.) --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer. -- Michael P. Butler in the LoungeHi Mike, and thanks for your answer, what i ment by "instances" of methods was actually a "copy" of it on memory during program execution, NE way, i know now that each thread has its own space, and there for receives its own "copy" of the function. Thanks!
-
Hi Mike, and thanks for your answer, what i ment by "instances" of methods was actually a "copy" of it on memory during program execution, NE way, i know now that each thread has its own space, and there for receives its own "copy" of the function. Thanks!
You're thinking of stacks. Each thread has its own stack, and its own copy of local variables in the function. Aside from that, you're on target :) --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber | RightClick-Encrypt You cannot stop me with paramecium alone!