[sldev] Re: Threading in Second Life

Michael Miller 1337mail at gmail.com
Sat Aug 11 23:33:21 PDT 2007


Can anyone give me a general overview of threading/locks in second
life? What I did now was I ran my program during the beginning of the
main_loop() with no threads(I removed the while(true) loop). However,
I am still getting problems. For example, as soon as the client
launches, it crashes at gMenuHolder->hideMenus(); in the resetView()
method, even if I pause the texture cache and image decoding threads
when doing this.

I get the feeling I am taking a VERY wrong approach in this. Is there
a 'clean' way I can deal with this ensuring that my code doesn't crash
the SL client(due to concurrent access of variables)? Would using the
LLWorkerThread class help? If so, how do I do this?

Thanks,
Mike

On 8/10/07, Michael Miller <1337mail at gmail.com> wrote:
> Right now, I'm modifying the SL client to have a loiter thread-
> basically just to move around, and rotate when something is in its
> way. However, I am encountering some difficulties when implementing
> this(using the boost threading library). Let me first give you a brief
> overview of the essential code I modified:
>
> In viewer.cpp global variable section:
> LoiterThread loiterThread;
>
> In viewer.cpp right before main_loop() is executed:
> boost::thread threadone(boost::bind(&LoiterThread::runLoop, &loiterThread));
>
> Added new file LoiterThread.cpp:
> #include "llviewerprecompiledheaders.h"
> #include "LoiterThread.h"
> #include "llagent.h"
> #include "llstartup.h"
> #include "v3math.h"
> #include <ctime>
> bool move = true;
>        LoiterThread::LoiterThread()
>        {
>
>        }
>        void LoiterThread::runLoop(){
>                LLVector3 temp;
>                LLQuaternion temp1;
>                int lastTime=0;
>                while(true){
>                        if(gStartupState == STATE_STARTED && move == true){
>                                if(temp == gAgent.getPositionAgent() &&
> temp1!=gAgent.getHeadRotation() && time(0)>(lastTime+2)){ //If we're
> in the same position for more than 2 seconds, we need to rotate
>                                        gAgent.moveYaw(5.f);
>                                        lastTime = time(0);
>                                }
>                                //llinfos << "Pos: " <<
> gAgent.getPositionAgent() << llendl;
>                                gAgent.moveAt(1); //Move forward
>                                temp = gAgent.getPositionAgent();
>                                temp1 = gAgent.getHeadRotation();
>                        }
>                        Sleep(10);
>                }
>        }
>
> Added new file LoiterThread.h:
> #ifndef H_LOITERTHREAD
> #define H_LOITERTHREAD
>
> class LoiterThread{
> public:
> LoiterThread();
> void runLoop();
> bool move;
> };
> #endif
>
> Now, the problem arises with gAgent.moveYaw and gAgent.moveAt(though
> primarily with the former). These methods are not thread safe and
> cause the client to crash occasionally. I saw that there was a custom
> threading class(llthread.cpp). Should I be using this instead of the
> boost libraries? If so, can someone give me a basic overview of how
> this would work?
>
> Thanks a ton,
> Mike
>


More information about the SLDev mailing list