[SOLVED] Posix Threads

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

[SOLVED] Posix Threads

Post 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.
Last edited by Moosader on Fri Oct 09, 2009 9:28 am, edited 1 time in total.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Posix Threads

Post by short »

Maybe you have yet to see this usage example?

http://www.google.com/codesearch/p?hl=e ... ead_create
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Re: Posix Threads

Post by Moosader »

<qpHalcy0n1> void* MyFunction( void* ) { stuff }
<qpHalcy0n1> you missed the void ptr...
solved, thanks :P
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Posix Threads

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply