www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Something weird with threads.

reply BizarreCake <bizarrecake gmail.com> writes:
While messing with threads from std.concurrency, I've been getting
this weird problem.

The code below, runs perfectly fine.

-------------------------------------------
  import std.concurrency;

  void main( ) {
    spawn( &func );
  }

  void func( ) {

  }
--------------------------------------------

But when func( ) is moved to a class:

-------------------------------------------
  import std.concurrency;

  void main( ) {
    auto c = new C;
    spawn( &c.func );
  }

  class C {
    void func( ) {

    }
  }
--------------------------------------------

doesn't compile at all. It produces the following errors:
  - Error: template std.concurrency.spawn(T...) does not match any
function template declaration

  - Error: template std.concurrency.spawn(T...) cannot deduce
template function from argument types !()(void delegate())

Am I doing it wrong? Is there a workaround?
Thanks.
Aug 07 2011
parent Trass3r <un known.com> writes:
   - Error: template std.concurrency.spawn(T...) cannot deduce
 template function from argument types !()(void delegate())

As the message already tells a class method yields a delegate and not a function. Check if that method could be made static. But if you really need to do it this way, you may create a static wrapper function that calls the method on a particular class instance. You should be able to use a function literal as well.
Aug 07 2011