Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

how to use tango.stdc.posix.pthread ?

Moderators: larsivi kris

Posted: 11/10/07 22:24:25

Hello, how to create a new thread ? I wrote something like that:

import tango.stdc.posix.pthread;

void*  f() {
        return null;

void main() {
        pthread_t t;
        pthread_attr_t attr;
        pthread_create(&t, &attr, f, null);



th.d(10): function tango.stdc.posix.pthread.pthread_create (uint*,pthread_attr_t *,void*(C *)(void*),void*) does not match parameter types (uint*,pthread_attr_t *,void*,void*)
th.d(10): Error: cannot implicitly convert expression (f()) of type void* to void*(C *)(void*)

Author Message

Posted: 11/10/07 23:46:37 -- Modified: 11/10/07 23:47:32 by

Is there a reason why you're not using tango.core.Thread instead?

(btw: you need &f, I think)

Posted: 11/11/07 01:04:18

Hmm, I gave bad example. I have something like that in C:

#include <pthread.h>
#include <stdio.h>

int f(int arg) {
        printf("x=%d", arg);
int main() {
        int x = 123;
        pthread_t thread;
        pthread_create( &thread, 0, &f, x );
        return 0;
gcc -lpthread pthread.c

It is possible to do it using tango.core.Thread ? Or any other way ?

// not work
(new Thread ( &func(arg1) )).start;

Posted: 11/11/07 06:32:16

I think the problem is that you are trying to pass an argument to the thread. I believe the function signature has to be:

void f(){...}

So how do you pass data to the thread? Here's one idea (didn't actually compile this, so may have a syntax error):

import tango.core.Thread;

int f(int arg) {
int main(char[][]args) {
        int x = 123;
        void run() { f(x);}
        Thread thread = new Thread(&run);
        return 0;

Notice how the nested function run still has access to the main function's variables. You can also pass a member function of a class, which then has access to the class. These are actually delegates, and are much more useful than function pointers :)