www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Whitch can replace std::bind/boost::bind ?

reply Dsby <dushibaiyu yahoo.com> writes:
foreach (i ; 0..4) {
	auto th = new Thread(delegate(){listRun(i);});//this is erro
	_thread[i]= th;
	th.start();
}

void listRun(int i)
{
      writeln("i = ", i); // the value is not(0,1,2,3), it all is 
2.
}


I want to know how to use it like std::bind.
Mar 18 2016
next sibling parent reply Atila Neves <atila.neves gmail.com> writes:
On Friday, 18 March 2016 at 10:50:34 UTC, Dsby wrote:
 foreach (i ; 0..4) {
 	auto th = new Thread(delegate(){listRun(i);});//this is erro
 	_thread[i]= th;
 	th.start();
 }

 void listRun(int i)
 {
      writeln("i = ", i); // the value is not(0,1,2,3), it all 
 is 2.
 }


 I want to know how to use it like std::bind.
I would suggest not using Thread directly: foreach(i; 0..4) { auto tid = spawn(&listRun, i); //from std.concurrency _tid[i] = tid; } Atila
Mar 18 2016
parent reply Dsby <dushibaiyu yahoo.com> writes:
On Friday, 18 March 2016 at 11:09:37 UTC, Atila Neves wrote:
 On Friday, 18 March 2016 at 10:50:34 UTC, Dsby wrote:
 foreach (i ; 0..4) {
 	auto th = new Thread(delegate(){listRun(i);});//this is erro
 	_thread[i]= th;
 	th.start();
 }

 void listRun(int i)
 {
      writeln("i = ", i); // the value is not(0,1,2,3), it all 
 is 2.
 }


 I want to know how to use it like std::bind.
I would suggest not using Thread directly: foreach(i; 0..4) { auto tid = spawn(&listRun, i); //from std.concurrency _tid[i] = tid; } Atila
the listrun is in class. it is a delegate,it is not a function.
Mar 18 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/18/2016 09:14 AM, Dsby wrote:
 On Friday, 18 March 2016 at 11:09:37 UTC, Atila Neves wrote:
 On Friday, 18 March 2016 at 10:50:34 UTC, Dsby wrote:
 foreach (i ; 0..4) {
     auto th = new Thread(delegate(){listRun(i);});//this is erro
     _thread[i]= th;
     th.start();
 }

 void listRun(int i)
 {
      writeln("i = ", i); // the value is not(0,1,2,3), it all is 2.
 }


 I want to know how to use it like std::bind.
I would suggest not using Thread directly: foreach(i; 0..4) { auto tid = spawn(&listRun, i); //from std.concurrency _tid[i] = tid; } Atila
the listrun is in class. it is a delegate,it is not a function.
Here is one that puts 'shared' in a lot of places: import std.stdio; import std.concurrency; class C { int i; this (int i) shared { this.i = i; } void listRun() shared { writeln("listRun for ", i); } } void worker(shared(C) c) { c.listRun(); } void main() { Tid[4] _tid; foreach(i; 0..4) { auto c = new shared(C)(i); auto tid = spawn(&worker, c); _tid[i] = tid; } } Here is an equivalent that casts to and from 'shared' before and after the thread call: import std.stdio; import std.concurrency; class C { int i; this (int i) { this.i = i; } void listRun() { writeln("listRun for ", i); } } void worker(shared(C) c_shared) { auto c = cast(C)c_shared; c.listRun(); } void main() { Tid[4] _tid; foreach(i; 0..4) { auto c = new C(i); auto c_shared = cast(shared(C))c; auto tid = spawn(&worker, c_shared); _tid[i] = tid; } } Ali
Mar 18 2016
parent Dsby <dushibaiyu yahoo.com> writes:
On Friday, 18 March 2016 at 17:31:11 UTC, Ali Çehreli wrote:
 On 03/18/2016 09:14 AM, Dsby wrote:
 [...]
Here is one that puts 'shared' in a lot of places: import std.stdio; import std.concurrency; [...]
thanks.
Mar 19 2016
prev sibling next sibling parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Friday, 18 March 2016 at 10:50:34 UTC, Dsby wrote:
 foreach (i ; 0..4) {
 	auto th = new Thread(delegate(){listRun(i);});//this is erro
 	_thread[i]= th;
 	th.start();
 }

 void listRun(int i)
 {
      writeln("i = ", i); // the value is not(0,1,2,3), it all 
 is 2.
 }


 I want to know how to use it like std::bind.
This is a bug in the compiler: https://issues.dlang.org/show_bug.cgi?id=2043
Mar 18 2016
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/18/2016 03:50 AM, Dsby wrote:
 foreach (i ; 0..4) {
      auto th = new Thread(delegate(){listRun(i);});//this is erro
      _thread[i]= th;
      th.start();
 }

 void listRun(int i)
 {
       writeln("i = ", i); // the value is not(0,1,2,3), it all is 2.
 }


 I want to know how to use it like std::bind.
A workaround is an intermediate function that returns the delegate: import std.stdio; import core.thread; void listRun(int i) { writeln("i = ", i); // the value is not(0,1,2,3), it all is 2. } auto makeClosure(int i) { return delegate(){listRun(i);}; } void main() { Thread[4] _thread; foreach (i ; 0..4) { auto th = new Thread(makeClosure(i)); _thread[i]= th; th.start(); } } Prints different values: i = 1 i = 0 i = 2 i = 3 Ali
Mar 18 2016
parent Dsby <dushibaiyu yahoo.com> writes:
On Friday, 18 March 2016 at 17:24:27 UTC, Ali Çehreli wrote:
 On 03/18/2016 03:50 AM, Dsby wrote:
 foreach (i ; 0..4) {
      auto th = new Thread(delegate(){listRun(i);});//this is 
 erro
      _thread[i]= th;
      th.start();
 }

 void listRun(int i)
 {
       writeln("i = ", i); // the value is not(0,1,2,3), it all 
 is 2.
 }


 I want to know how to use it like std::bind.
A workaround is an intermediate function that returns the delegate: import std.stdio; import core.thread; void listRun(int i) { writeln("i = ", i); // the value is not(0,1,2,3), it all is 2. } auto makeClosure(int i) { return delegate(){listRun(i);}; } void main() { Thread[4] _thread; foreach (i ; 0..4) { auto th = new Thread(makeClosure(i)); _thread[i]= th; th.start(); } } Prints different values: i = 1 i = 0 i = 2 i = 3 Ali
Thanks for your mind. i write the bind function: import std.stdio; import core.thread; import std.functional; class AA { void show(int i) { writeln("i = ", i); // the value is not(0,1,2,3), it all is 2. } } void listRun(int i) { writeln("i = ", i); } void main() { Thread[4] _thread; Thread[4] _thread2; AA a = new AA(); foreach (i ; 0..4) { auto th = new Thread(bindDg(&a.show,i)); _thread[i]= th; auto th2 = new Thread(bindFun!listRun(i + 10)); _thread2[i]= th2; } foreach(i;0..4) { _thread[i].start(); } foreach(i;0..4) { _thread2[i].start(); } } auto bindDg(T, Args...)(T fun,Args args) if (is(T == delegate) || is(T == function)) { return delegate(){return fun(forward!args);}; } auto bindFun(alias Fun,Args...)(Args args) { return delegate(){return Fun(forward!args);}; } my value is : i = 2 i = 0 i = 3 i = 11 i = 13 i = 10 i = 12 i = 1
Apr 22 2016