IS this multi-task safe

J

John Black

Hi,
I want to use static member variable to monitor the execution state,
but I am not sure this is multi-thread or multi task safe. Here is my
class:

calss MyClass{
static int numCandidates;

public:
MyClass();
int func();
};

MyClass::MyClass(){
... get a list of candidate algorithms...
... set the size of the list to numCandidates...
}

int MyClass::func(){
If (numCandidates > 0){
... try the nth candidate algorithm...
--numCandidates;
...return algorithm result...
}

return -1;
}

main(){
MyClass::numIns = 0;

MyClass c;

int r;

while ((r=c.func()) != -1){
... some analysis of the algorithm result ...
}
}

You can see that I use MyClass::numCandidates to monitor the current
algorithm. My question is if these is any hole for this when there are
multiple copies started at the same time? Will each program has its owm
copy of MyClass::numCandidate?

Thanks.
 
V

Victor Bazarov

John said:
I want to use static member variable to monitor the execution state,
but I am not sure this is multi-thread or multi task safe. [...]

There is no way to tell in C++ terms. C++ _language_ does not define
multithreading or multitasking, that's the OS responsibility. AFAIK,
to be safe, any access to any shared memory should be in a critical
section, but then again, it has nothing to do with C++ _language_.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top