IS this multi-task safe

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Black

    #1

    IS this multi-task safe

    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::MyClas s(){
    ... 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::numCan didates 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::numCan didate?

    Thanks.

  • Victor Bazarov

    #2
    Re: IS this multi-task safe

    John Black wrote:[color=blue]
    > 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. [...][/color]

    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

    Comment

    Working...