core.thread 正确传参数方式

ww21xx 2012-03-09
线程:
class WorkerThread : Thread
{
	this(HANDLE handle, uint handleId)
	{
		this.m_handle = handle;
		this.m_handleId = m_handleId;
		super( &runWorker );
	}

private:
	uint m_handleId;
	HANDLE m_handle;
	void runWorker()
	{
		writefln("Thread running m_handle=>" ~ to!(string)(m_iocpHandle) ~ " m_handleId=>" ~ to!(string)(m_handleId));
	}
}

执行:
auto t = new WorkerThread(&m_iocpHandle, 1983);
t.start();

结果:
Thread running m_handle=>0 m_handleId=>0


    。。。。。。。。。。。。。。。。。。。
ilovetopview 2012-03-10
super是传给基类了,相当于没传啊,当然是空了。
flythink 2012-03-10
什么传给基类了, 什么逻辑啊.

问题应该出在2个变量的生存周期这吧,

线程这个用法是没问题的

class DerivedThread : Thread
{
     this(int xx)
     {
         this._xx = xx;
         super( &run );
     }

private :
     int _xx;

     void run()
     {
         writefln( to!string(_xx) );
     }

}
void main()
{
   auto t = new DerivedThread(33);
   t.start();
   t.join();
}
ww21xx 2012-03-10
flythink 写道
什么传给基类了, 什么逻辑啊.

问题应该出在2个变量的生存周期这吧,

线程这个用法是没问题的

class DerivedThread : Thread
{
     this(int xx)
     {
         this._xx = xx;
         super( &run );
     }

private :
     int _xx;

     void run()
     {
         writefln( to!string(_xx) );
     }

}
void main()
{
   auto t = new DerivedThread(33);
   t.start();
   t.join();
}


我不得不说 你太厉害了。。。 昨天刚刚看到老外的另外一种写法。。。今天又到你回复。。。  
ww21xx 2012-03-10
继承传参数方法是完全对的,我上面的代码有错,所以导致返回是空的!
应该是:
class MyWorkerThread : Thread
	{
	    this(HANDLE handle, uint handleId)
	    {
	        this.m_handle = handle;
	        this.m_handleId = handleId;
	        super( &runWorker );
	    }
	
	private:
	    uint m_handleId;
	    HANDLE m_handle;
	    void runWorker()
	    {
	        writefln("Thread running m_handle=>" ~ to!(string)(m_handle) ~ " m_handleId=>" ~ to!(string)(m_handleId));
	    }
	}


另外,我最早的原因的确是变量周期的问题
Global site tag (gtag.js) - Google Analytics