digitalmars.D.learn - Whitch can replace std::bind/boost::bind ?
- Dsby (11/11) Mar 18 2016 foreach (i ; 0..4) {
- Atila Neves (7/18) Mar 18 2016 I would suggest not using Thread directly:
- Dsby (2/24) Mar 18 2016 the listrun is in class. it is a delegate,it is not a function.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (51/77) Mar 18 2016 Here is one that puts 'shared' in a lot of places:
- Dsby (2/8) Mar 19 2016 thanks.
- Marc =?UTF-8?B?U2Now7x0eg==?= (3/14) Mar 18 2016 This is a bug in the compiler:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (25/35) Mar 18 2016 A workaround is an intermediate function that returns the delegate:
- Dsby (54/99) Apr 22 2016 Thanks for your mind.
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
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
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:the listrun is in class. it is a delegate,it is not a function.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
On 03/18/2016 09:14 AM, Dsby wrote:On Friday, 18 March 2016 at 11:09:37 UTC, Atila Neves wrote: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; } } AliOn Friday, 18 March 2016 at 10:50:34 UTC, Dsby wrote:the listrun is in class. it is a delegate,it is not a function.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
On Friday, 18 March 2016 at 17:31:11 UTC, Ali Çehreli wrote:On 03/18/2016 09:14 AM, Dsby wrote:thanks.[...]Here is one that puts 'shared' in a lot of places: import std.stdio; import std.concurrency; [...]
Mar 19 2016
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
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
On Friday, 18 March 2016 at 17:24:27 UTC, Ali Çehreli wrote:On 03/18/2016 03:50 AM, Dsby wrote: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 = 1foreach (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
Apr 22 2016