Page 1 of 1

[SOLVED] Posix Threads

Posted: Wed Oct 07, 2009 11:20 pm
by Moosader
I'm stuck. I'm starting off with Posix threads for a school assignment, and I'm confused about what's not working right.

Basically, I'm just trying to use pthread_create, and I can't get the last argument to work right. I've seen some examples that have passed NULL, but that doesn't work for me, and other places I've seen chars or ints, and I [think] I did it exactly the same, but I keep getting errors that say something like:

|error: invalid conversion from ‘void (*)(void*)’ to ‘void* (*)(void*)’|

Here's what I have atm:

Code: Select all

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

void MyFunction( void *data )
{
    int tehData = (int)data;
    printf( "Function %i", data );
    pthread_exit( NULL );
}

int main( int argc, char *args[] )
{
    pthread_t thread;
    int iThreadId;
    int arg = 11;

    iThreadId = pthread_create( &thread, NULL, MyFunction, (void*)arg );

    pthread_join( thread, NULL );

    printf( "Return %i", iThreadId );

    return 0;
}
If I try using "NULL" in place of "(void*)arg", I get

|error: invalid conversion from ‘void (*)(void*)’ to ‘void* (*)(void*)’|

So I don't know what I'm doing wrong.

Re: Posix Threads

Posted: Wed Oct 07, 2009 11:32 pm
by short
Maybe you have yet to see this usage example?

http://www.google.com/codesearch/p?hl=e ... ead_create

Re: Posix Threads

Posted: Wed Oct 07, 2009 11:37 pm
by Moosader
<qpHalcy0n1> void* MyFunction( void* ) { stuff }
<qpHalcy0n1> you missed the void ptr...
solved, thanks :P

Re: Posix Threads

Posted: Thu Oct 08, 2009 10:38 am
by MarauderIIC
FYI, from experience
cout is not thread-safe using POSIX on Windows XP
cout is thread-safe using POSIX on Linux. Or at least it looked that way when I was using Mandrake.

ie:
thread1: cout << "Hello" << endl;
thread2: cout << "There" << endl;

Windows:
'Hethellore

'

Linux
There
Hello

or
Hello
There