CMutex - Programmer

This is a discussion on CMutex - Programmer ; I am trying to use a CMutex to stop a thread from being created twice. So I have this: if ( this->m_AlertRunning.Lock(0) ) /* Don't wait at all */ { this->m_AlertRunning.Lock(1000); AfxBeginThread( StaticFlashThread, this ); // Start flasher thread } ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: CMutex

  1. CMutex

    I am trying to use a CMutex to stop a thread from being created twice.

    So I have this:

    if ( this->m_AlertRunning.Lock(0) ) /* Don't wait at all */
    {
    this->m_AlertRunning.Lock(1000);
    AfxBeginThread( StaticFlashThread, this ); // Start flasher thread
    }


    Now when the thread exits, it calls m_AlertRunning.Unlock(), the thing is
    that the Mutex doesn't appear to work becuase m_AlertRunning.Lokc( 0 )
    always returns true no matter how many times it's called.

    I don't want to use CSingleLock becuase I would then need two of these in
    the class, one in the thread and one in the above function.



  2. Re: CMutex

    Have you tried using a CCriticalSection ? You can just declare an object
    of it and then use Lock() and Unlock() on it.

    "Spacen Jasset" wrote in message
    > I don't want to use CSingleLock becuase I would then need two of these in
    > the class, one in the thread and one in the above function.


  3. Re: CMutex


    "Fritz Feuerbacher" wrote in message
    news:1f70be85.0407312154.6f16d518@posting.google.c om...
    > Have you tried using a CCriticalSection ? You can just declare an object
    > of it and then use Lock() and Unlock() on it.
    >
    > "Spacen Jasset" wrote in message
    > > I don't want to use CSingleLock becuase I would then need two of these

    in
    > > the class, one in the thread and one in the above function.


    Ok. I will try that. -- I've just used a common variable for now, as there
    is no serious race condition. Is CMutex not ment to be used in that way?



  4. Re: CMutex


    "Spacen Jasset" wrote in message
    news:410d641c$0$6441$cc9e4d1f@news-text.dial.pipex.com...
    >
    > "Fritz Feuerbacher" wrote in message
    > news:1f70be85.0407312154.6f16d518@posting.google.c om...
    > > Have you tried using a CCriticalSection ? You can just declare an

    object
    > > of it and then use Lock() and Unlock() on it.
    > >
    > > "Spacen Jasset" wrote in message
    > > > I don't want to use CSingleLock becuase I would then need two of these

    > in
    > > > the class, one in the thread and one in the above function.

    >
    > Ok. I will try that. -- I've just used a common variable for now, as there
    > is no serious race condition. Is CMutex not ment to be used in that way?
    >
    >

    I remember why I didn't try CCriticalSeciton now -- you cannot use timed
    waits. I do no want to wait for the lock object to become available.



+ Reply to Thread