[sldev] Threading in Second Life

Michael Miller 1337mail at gmail.com
Fri Aug 10 16:57:39 PDT 2007


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