How can a function pointer required to be extern(C)?

rempas rempas at tutanota.com
Wed Apr 12 20:23:51 UTC 2023


Sorry if the title doesn't make any sense, let me explain. So, I 
do have the following code that does not compile:

```d
import core.sys.posix.pthread; /* The library */

struct Thread {
private:
   pthread_t thread_id;

public:
   this(void* function(void*) func, void* arg = null, scope 
const(pthread_attr_t*) attr = null) {
     pthread_create(&this.thread_id, attr, func, arg);
   }

   @property:
     pthread_t id() { return this.thread_id; }
}

```

Yes, I'm trying to "encapsulate" the Pthread (POSIX threads) API. 
Normally, the function pointer that is passed to "pthread_create" 
must be "extern(C)" and this is the complaining that the compile 
does. So, I'm thinking to replace the constructor to this:

```d
this(extern(C) void* function(void*) func, void* arg = null,
      scope const(pthread_attr_t*) attr = null)
{ pthread_create(&this.thread_id, attr, func, arg); }
```

I just added "extern(C)" before the type. This is how it looks in 
the error message so it must work right? Well... it doesn't. And 
here I am wondering why. Any ideas?


More information about the Digitalmars-d-learn mailing list