Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » MT again
Re: MT again (SOLVED!) [message #28174 is a reply to message #24029] Fri, 20 August 2010 11:45 Go to previous messageGo to previous message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
Well, just for reference I'll post a bug that drove me crazy for long time Smile
Here the sample code :

void ThreadCb(void)
{
    INTERLOCKED_(mutex) {
        inThread = true;
    }

    << DO SOME TIME CONSUMING STUFFS>

    INTERLOCKED_(mutex) {
        inThread = false;
}

bool StartThread(void)
{
    bool inTh;
    INTERLOCKED_(mutex) {
        inTh = inThread;
    }
    if(inTh)
        return false;
    myThread.Run(ThreadCb);
    return true;
}



Purpose is clear, just start the thread *ONLY* if not already running, return true if success or false if thread was already running.
Where's the caveat ? It's simply in the time BETWEEN the start of ThreadCb() callback and the setting of 'inThread' variable to true. If code calls fast enough the StartThread() routine, it can happen that the Thread callback is in the middle of startup code BUT has still NOT SET the 'inThread' flag; so the StartThread() routine believes thread isn't running and calls it again.
The solution :

void ThreadCb(void)
{
    << DO SOME TIME CONSUMING STUFFS>

    INTERLOCKED_(mutex) {
        inThread = false;
}

bool StartThread(void)
{
    bool inTh;
    INTERLOCKED_(mutex) {
        inTh = inThread;
    }

    if(inTh)
        return false;
    inThread = true;
    myThread.Run(ThreadCb);
    return true;
}



So, I've moved the 'inThread' flag SETTING from the thread routine to its caller; the reset still belong to thread routine.
Now it's the caller that has the responsibility of setting the variable, so it can't happen that thread routine performs some stuffs between check and launch.

Ciao

Max

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: HttpQuery - not working?
Next Topic: CGI-BIN bug with Apache Server
Goto Forum:
  


Current Time: Mon Apr 29 17:52:41 CEST 2024

Total time taken to generate the page: 0.02489 seconds