Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Programming

Journal Earthquake Retrofit's Journal: Adventures in modern programming

I learned to program in BASIC on a TRS-80 model 1 level 1. By the time I got level 2 BASIC I was already exploring machine language. I actually hand assembled some stuff. Later I learned PL/I, Pascal, assembly in IBM 370 (got an A) and, god help me, COBOL at university. But that was all thirty years ago and only had a Commodore well into the 1990s.

I recently watched the lectures at the M.I.T. website for the introductory C++ course. The professor was quite entertaining but wasn't too useful on a practical scale. No doubt the labs are much more informative. But it got me inspired to learn a modern language. The Microsoft APIs were enough to chase me away from assembly for a while. And got me to install Linux.

I'm not completely clueless and I've read all about Java since it started and OOP. And I never quite got it. Lots and lot of tutorials exist on the web, some of which are worse than useless for a beginner. But this time I wasn't going to be discouraged. I got going and decided I would make a spaceship. Threads, I think. Now that's something I can have fun with.

At "http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-18.htm", I found this mess:

class Thread
{
      public:
            Thread();
            int Start(void * arg);
      protected:
            int Run(void * arg);
            static void * EntryPoint(void*);
            virtual void Setup();
            virtual void Execute(void*);
            void * Arg() const {return Arg_;}
            void Arg(void* a){Arg_ = a;}
      private:
            THREADID ThreadId_;
            void * Arg_;
};
Thread::Thread() {}
int Thread::Start(void * arg)
{
      Arg(arg); // store user data
      int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
      return code;
}
int Thread::Run(void * arg)
{
      Setup();
      Execute( arg );
} /*static */
void * Thread::EntryPoint(void * pthis)
{
      Thread * pt = (Thread*)pthis;
      pthis->Run( Arg() );
}
virtual void Thread::Setup()
{ // Do any setup here
}
virtual void Thread::Execute(void* arg)
{ // Your code goes here
}

Forty three lines. All this to implement a thread? Oh, well look at all the difficulties you have to overcome:

"The create a thread, you must specify a function that will become the entry point for the thread. At the operating system level, this is a normal function. We have to do a few tricks to wrap a C++ class around it (*) because the entry function cannot be a normal member function of a class. However, it can be a static member function of a class. This is what we will use as the entry point. There is a gotcha here though. Static member functions do not have access to the this pointer of a C++ object. They can only access static data. Fortunately, there is way to do it. Thread entry point functions take a void * as a parameter so that the caller can typecast any data and pass in to the thread. We will use this to pass this to the static function. The static function will then typecast the void * and use it to call a non static member function."

* He doesn't say why on Earth I would want to...

A FEW tricks he says and a gotcha. And still does nothing yet. Now look at this thread declaration:

void*
shield_running(void* data) {

        float fuel_usage = .0195; //rate of use kilos per minute

      while (Shield.power_on == true){
        Shield.min_of_operation = (Shield.min_of_operation + (1/60));
        Cargo.fuel = (Cargo.fuel - fuel_usage);
        Air.dust = (Air.dust + .0001);
        if (Shield.boost_on == true) {Cargo.fuel = (Cargo.fuel - fuel_usage);};
        sleep(1); };
        pthread_exit(NULL);
                      };

which is started by the following:

if (choice == "so") {Shield.power_on = true;
        choice = "0";
        thr_id = pthread_create(&p_thread, NULL, shield_running, (void*)&g);};

Only seventeen lines, it actually does something, is very easy to understand, was easy to adapt to other systems like my oxygen generator, and if I had 100 shields I could start a hundred threads. My teachers at SIU would not have liked the lack of comments, but C++ is almost self-documenting. And if this is actually true: "The call to pthread_exit() Causes the current thread to exit and free any thread-specific resources it is taking." then there should be no problem with memory leaks which I know jack about.

Both are written in the same C++. That's why I decided to learn C++. I could do it my way. I get the first example, Now. But I LEARNED from:

http://users.actcom.co.il/~choo/lupg/tutorials/multi-thread/multi-thread.html#definition
This one has clearly explained concepts and example programs that ACTUALLY COMPILE!

'Public' and 'private' and 'restricted'? Global and local are good enough for the likes of me. But I'm not totally close-minded. I suspect that a graphic user interface, which every modern spaceship should have, may actually require the use of classes. If so, like the way Republicans say they voted for McCain, I'll hold my nose and do it.

Now if only I could find a simple example of a C++ program with a simple GUI that ACTUALLY COMPILES on Kdevelop, I might have enough fun for another thirty years. Tonight... make that tomorrow, I'm trying out QT designer.
 

This discussion has been archived. No new comments can be posted.

Adventures in modern programming

Comments Filter:

So you think that money is the root of all evil. Have you ever asked what is the root of money? -- Ayn Rand

Working...