Concurrency Issue

Posts 1–15 of 17 · Page 1 of 2
Concurrency Issue
Well its not really an issue yet, but its definitely possible.

K say I have 2 threads.

Thread one uses these objects to do stuff and call their functions.

Thread two deletes the objects not in use So thread one can worry about other things.

Now say thread two deletes a object while thread one is using it. Now thread one checks to make sure the the object isn't deleted before it calls its functions, but with multiple cores, its possible thread 2 deleted thread 1's object immediately after it checked. Is there a full proof way to make sure thread one is never using something thread two deleted?
declare a global variable and use it in both threads to check whether its done with the object or not?
Well I do check that. Hmmm.... actually nvm. The chances of it occuring was already pretty low, but I just realised the function that uses the object resets its timer, so that it would have to wait at minimum 12 milliseconds before the Thread2 could refilter the object that was just used, but the other object can not be being used while this function is called so I know its okay to filter them... whew. Ok Thanks Astral, disregard this post. I post strange things when sleepy. o_O

AHHH! 666
is 12 milliseconds a set time by the CPU or did you just measure it? if you measured it, i believe it would vary between different CPUs

but idk
Nah that's the Minimum amount of time you can call Sleep() So Im hoping 12 ms is enough for any D3DXFONT function to execute. Unless someone tries to write something like 1000 lines of text, in which case they should increase the sleep time to account for that. nvm though, Im just rambling here. Sometimes I like to talk outload (or type outloud )... helps me think.
make your own sleep function with jumps and rdtsc, idk if it will be faster tho. i would assume it would be really accurate
Can you not make a (doesn't know what it'd be called in C++).. but it would be some form of array/vector that contains the object, and also a corresponding boolean value. When the function is using the object, set it's corresponding boolean value to true, and set it to false at the conclusion of the function. In the second thread, check whether the object's boolean value is false before deleting. In VB it would be a dictionary(Of Object, Boolean)
Code:
template<typename T>
class Object
{
public:
    Object(T *Object)
    {
        this->Object = Object;
    }

    bool IsInUse(void)
    {
        return this->bInUse;
    }

    void StartUsing(void)
    {
        this->bInUse=true;
    }

    void StopUsing(void)
    {
        this->bInUse=false;
    }

    T GetObject(void)
    {
        return this->Object;
    }

private:
    T *Object;
    bool bInUse;
};
Object->GetObject<int>();

Or something along those lines ¯\(o.O)/¯ I should learn how to use templates =3
Appreciate the help, but I figured it out already. I ended up doing something like that you all suggested with Boolean checks on each function.
As I understand it you're still not secure, wrap those checks in SEH's because if thread 1 decides to set the boolean and thread 2 decides to read it at the same time -> Boom! deadlock -> appcrash
Quote Originally Posted by .::SCHiM::. View Post
As I understand it you're still not secure, wrap those checks in SEH's because if thread 1 decides to set the boolean and thread 2 decides to read it at the same time -> Boom! deadlock -> appcrash
i dont think SEH is needed here, he can just use __try & __except
Hmmm... So far the current set up has been working fine, but if I find this becomes a problem I'll fix it. That would be extremely unlikely for two processors set and read the same boolean at the same time, but Im not sure what kind of consequences it would cause, because I'm not sure how multicore processors work.
Quote Originally Posted by why06 View Post
Hmmm... So far the current set up has been working fine, but if I find this becomes a problem I'll fix it. That would be extremely unlikely for two processors set and read the same boolean at the same time, but Im not sure what kind of consequences it would cause, because I'm not sure how multicore processors work.
i dont think it would cause anything major, but of course, its always better to be safe than sorry

[php]
__try
{
//do whatever with your object
}
__except(EXCEPTION_CONTINUE_EXECUTION)
{

}
[/php]
Quote Originally Posted by why06 View Post
Hmmm... So far the current set up has been working fine, but if I find this becomes a problem I'll fix it. That would be extremely unlikely for two processors set and read the same boolean at the same time, but Im not sure what kind of consequences it would cause, because I'm not sure how multicore processors work.
Shaun got a new avatar? Oh noes the world is gon explode
Quote Originally Posted by fukojr2 View Post


Shaun got a new avatar? Oh noes the world is gon explode
Lol. This is my old avatar. Well its new, but I always do Magnifying glasses. That's why HD's sig is like that.
Posts 1–15 of 17 · Page 1 of 2

Post a Reply

Tags for this Thread

None

Talk with us