Thread overview
Threads & fibers
Jan 30, 2011
Nrgyzer
Jan 30, 2011
Simen kjaeraas
Jan 30, 2011
Nrgyzer
January 30, 2011
Hey guys,

I already posted a thread in the wrong section (digitalmars.D instead of digitalmars.D.learn) - sorry for that. I'm looking for a solution to suspend/ interrupt threads which are sleeping.

In the last few minutes I figured out some things I didn't understand exactly. I tested thread and fibers from the core.thread-package.

My first test-code is the following:

import std.stdio;
import core.thread;

a testInstance;

class a {

	void writeTest() {

		writeln("test");

	}

}

void main(string[] args) {

	testInstance = new a();

	Thread t = new Thread(&threadFunc);
	t.start();

	Thread.yield(); // give the thread a chance to call threadFunc()

}

void threadFunc() {

	writeln(testInstance is null);

}

The result is: "true" which means that testInstance of type a is null - but I already created a instance and if I write "writeln(testInstance is null);" after Thread.yield(); in the main, it says "false" which means testInstance is a valid instance of the class a. -> Why does threadFunc() says true, when testInstance should be a valid instance of a?

Next question: When I extend my threadFunc()... like the following:

void threadFunc() {

	writeln(testInstance is null);
	Thread.sleep(milliseconds(10_000));

}

... is there any chance to interrupt the Thread.sleep-command or to suspend the
thread? As I know, the join()-method does wait until the thread is finished, but
does not interrupt the sleep()-command.

I hope anyone can help and know how I can do this all.

... sorry for double posting in digitalmars.d!

Thanks in advance!
January 30, 2011
Nrgyzer <nrgyzer@gmail.com> wrote:

> The result is: "true" which means that testInstance of type a is null - but I
> already created a instance and if I write "writeln(testInstance is null);" after
> Thread.yield(); in the main, it says "false" which means testInstance is a valid
> instance of the class a. -> Why does threadFunc() says true, when testInstance
> should be a valid instance of a?

The default storage in D is in TLS, that is, changes in one thread will
not be visible to others.

If instead you mark your class a as 'shared class a', it works the way
you'd expect it to.


> ... is there any chance to interrupt the Thread.sleep-command or to suspend the
> thread? As I know, the join()-method does wait until the thread is finished, but
> does not interrupt the sleep()-command.

I think the best way to do this would be using std.concurrency, and
passing it a message. Not sure, though.

-- 
Simen
January 30, 2011
> Nrgyzer <nrgyzer@gmail.com> wrote:
> > The result is: "true" which means that testInstance of type a is
null -
> > but I
> > already created a instance and if I write "writeln(testInstance is
> > null);" after
> > Thread.yield(); in the main, it says "false" which means
testInstance is
> > a valid
> > instance of the class a. -> Why does threadFunc() says true, when
> > testInstance
> > should be a valid instance of a?
> The default storage in D is in TLS, that is, changes in one thread
will
> not be visible to others.
> If instead you mark your class a as 'shared class a', it works the
way
> you'd expect it to.
> > ... is there any chance to interrupt the Thread.sleep-command or
to
> > suspend the
> > thread? As I know, the join()-method does wait until the thread is
> > finished, but
> > does not interrupt the sleep()-command.
> I think the best way to do this would be using std.concurrency, and passing it a message. Not sure, though.

Thanks, marking a as shared class works :)... I already used threads in D1 but there as I just know - since 2.030 I need shared- decleration.