www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - delegates and threads

reply Johan Granberg <lijat.meREM OVEgmail.com> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I have been writing a function that takes an array of delegates and 
executes them in parallel. (attached)

It works sort of but I ran into problems when I tried to use delegates 
that referenced the frame/this pointer. (gdc-0.18 osx based on dmd 0.157)

Object a=new Object();
void foo(){printf("p");}//OK
void foo(){printf("%*s",a.toString());}//segfault

Is their anything in the language that prevents a thread from reading 
the stack of another through a pointer? (or is this a language/compiler 
bug) I have written a reduced test case bellow.

Thanks in advance.

//begin testcase
import parallel;

class A
{
	void b(){printf("boo\n");}
}
void main()
{
	A a=new A();
	void delegate()[] o;
	o~=delegate void()
	{
		printf("minsk\n");
	};
	o~=delegate void()
	{
		a.b();
	};
	o~=delegate void()
	{
		printf("foo\n");
	};
	printf("before\n");
	parallelize(o);
	printf("after\n");
}
//prints
Starting program: /Users/johan/Desktop/test/test
Reading symbols for shared libraries . done
before
minsk
foo

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
[Switching to process 3902 thread 0xf03]
_Dmain12__dgliteral2FZv () at test.d:17
17                      a.b();
//end
Jul 19 2006
parent reply pragma <pragma_member pathlink.com> writes:
In article <e9mc1n$8om$1 digitaldaemon.com>, Johan Granberg says...
void foo(){printf("%*s",a.toString());}//segfault

Just shooing from the hip, but you might want to try %.*s to print char[] (or just use writefln() instead). ;) - EricAnderton at yahoo
Jul 19 2006
parent reply Johan Granberg <lijat.meREM OVEgmail.com> writes:
pragma wrote:
 In article <e9mc1n$8om$1 digitaldaemon.com>, Johan Granberg says...
 void foo(){printf("%*s",a.toString());}//segfault

Just shooing from the hip, but you might want to try %.*s to print char[] (or just use writefln() instead). ;) - EricAnderton at yahoo

No it is not this that is the problem. This was just a test case. The problem is that the program segfaults when dereferencing the context pointer which for some reason is null. It happens both when it points to a stack frame and when it points to a class. (it the format string was the problem it would segfault in strlen instead by the way)
Jul 20 2006
parent S. Chancellor <dnewsgr mephit.kicks-ass.org> writes:
On 2006-07-20 00:41:20 -0700, Johan Granberg <lijat.meREM OVEgmail.com> said:

 pragma wrote:
 In article <e9mc1n$8om$1 digitaldaemon.com>, Johan Granberg says...
 void foo(){printf("%*s",a.toString());}//segfault

Just shooing from the hip, but you might want to try %.*s to print char[] (or just use writefln() instead). ;) - EricAnderton at yahoo

No it is not this that is the problem. This was just a test case. The problem is that the program segfaults when dereferencing the context pointer which for some reason is null. It happens both when it points to a stack frame and when it points to a class. (it the format string was the problem it would segfault in strlen instead by the way)

Did you figure out the answer to your problem? It may be an issue with mach threads.
Jul 22 2006