Add this blog to Del.icio.us, Digg or Furl | Create Watchlist for this blog

Archive for the ‘D’ Category

Multithreaded programming in D

Friday, June 8th, 2007

While in C++ there was no build-in threading support, D comes with a JAVA-like class “Thread”. So while in C/C++ you had to mingle around with threading libs like pthread or solaris-threads, in D you can leave that problem to phobos.

A simple example shows all the simplicity:

import std.c.time;
import std.stdio;
import std.thread;

int runThread(void *ptr)
{
  printf("Thread %d startedn", cast(int)ptr);
  sleep(10);
  printf("Thread %d endedn", cast(int)ptr);
  return 0;
}

int main()
{
  Thread t1 = new Thread(&runThread, cast(void *)1);
  t1.start();
  sleep(2);
  Thread t2 = new Thread(&runThread, cast(void *)2);
  t2.start();

  while ((t1.getState() == std.thread.Thread.TS.RUNNING)
            || (t2.getState() == std.thread.Thread.TS.RUNNING))
    Thread.yield ();   

  printf("program readyn");
  return 0;
}

That’s it. Save the code as ThreadTest.d, than compile with “gdc ThreadTest.d” and run a.out (or a.exe). What you get as output should be very similar to:

rainer@ThinkTop ~
$ ./a.exe
Thread 1 started
Thread 2 started
Thread 1 ended
Thread 2 ended
program ready

Programming in D - getting startet

Sunday, June 3rd, 2007

I am programming in C/C++ for more then 15 years now. I took a look at JAVA, programmed some applications for 5 years now. But I am not so enthusiastic with JAVA, I find it kind of childish - it never stepped out of that web application area (in my opinion). No question, JAVA is bad for system programming - and no question too, there are some better choices today for business legacy system than JAVA.
I think the programming world is moving to the component side (away from the one-for-all approach, but independent from the infrastructure system for now). So what I looked for is a modern language to program fast, system-near components. And I found D.

If you search for information D is a terrible name
Try “D” in google and you will get some billions of results. Too bad, we need a web-catalog for this topic, but start with wikipedia for a first try. You will find that …

D is imperative, object-oriented, and metaprogramming
In short, that means that D knows objects and templates.

Continue with “Hello World”
The usual hello world is pretty simple and pretty close to C:

import std.stdio; // writefln() needs this module
int main(char[][] args)
{
writefln("Hello World!");
}

As you see, the include “stdio.h” has been replaced by a import std.stdio (looks like we have a package-like concept here now). And the printf() is writefln() today.

But you need a D compiler now
So start with the dgcc project. They deliver for all major (and some minor) plattforms. Install the compiler, save the code from above as test.d then compile like “gdc test.d”. As result you get the well known a.out executable (or a.exe under windows).
Run a.out and you will get:

Hello World!
Error: AssertError Failure test.d(5)

That’s because you didn’t return from your main function, so D is more severe than C. Insert a return 0; after the writefln() line and you’re done.

Next post will start with a serious application: A TCP/IP server in D.