tags:

views:

910

answers:

6

I have no idea why this dosent work

#include <iostream>
#include <pthread.h>
using namespace std;

void *print_message(){

    cout << "Threading\n";
}



int main() {

    pthread_t t1;

    pthread_create(&t1, NULL, &print_message, NULL);
    cout << "Hello";

    return 0;
}

Error: [Description, Resource, Path, Location, Type] initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’ threading.cpp threading/src line 24 C/C++ Problem

+4  A: 

Because the main thread exits.

Put a sleep in the main thread.

cout << "Hello";
sleep(1);

return 0;

The POSIX standard does not specify what happens when the main thread exits.
But in most implementations this will cause all spawned threads to die.

So in the main thread you should wait for the thread to die before you exit. In this case the simplest solution is just to sleep and give the other thread a chance to execute. In real code you would use pthread_join();

#include <iostream>
#include <pthread.h>
using namespace std;

#if defined(__cplusplus)
extern "C"
#endif
void *print_message(void*)
{
    cout << "Threading\n";
}



int main() 
{
    pthread_t t1;

    pthread_create(&t1, NULL, &print_message, NULL);
    cout << "Hello";

    void* result;
    pthread_join(t1,&result);

    return 0;
}
Martin York
nope cause i have pthread_join(t1) at the end before return 0. Plus its giving me build errors and not runtime errors :(
Shahmir Javaid
well not really... cause the print_message takes an argument.. Dont want it to take an argument
Shahmir Javaid
You ninja-fixed the build error but didn't write about it, then went on to talk about another problem he'll have. You got marked down because someone (not me) didn't catch that build fix.
280Z28
+1 FLAME WAR! ah ha!
beggs
I had to bump you even though I think you're going to overtake my "lesser" answer lol
280Z28
@Martin York, Well the thing is when i look at this http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html the Mutexes Example shows the declaration of void *functionC() and then the functionC being used later in the thread... Thats what i was after
Shahmir Javaid
@Shahmir: But the first example on this page shows how to create a pthread and associated function correctly. From that you can call any other function.
Martin York
+8  A: 

You should declare the thread main as:

void* print_message(void*) // takes one parameter, unnamed if you aren't using it
280Z28
Thats the idea i don't want to put that as a parameter...the above way works
Shahmir Javaid
This is the correct answer. The original question was misformatted, so he got the error message wrong (missing a couple of asterisks, which were taken as italic markup). I've fixed it now.
Pavel Minaev
It doesn't matter what you *want*, `pthread_create` takes a pointer to a function that takes `void*` as a parameter and returns `void*`. It's been the defined API for decade(s). :) You aren't forced to use it though.
280Z28
ok but now lets say if i want to use the print_message as a function rather than thread how do i do that.. Its going to complain about the pointer
Shahmir Javaid
the answer to ^ == print_message(NULL)
Shahmir Javaid
A: 

When compiling with G++, remember to put the -lpthread flag :)

cwap
I think that might be the issue but im using eclipse any ideas where to add it
Shahmir Javaid
Just noticed ive already done that. :D otherwise it wont compile the pthread funcs
Shahmir Javaid
A: 

Linkage. Try this:

extern "C" void *print_message() {...

Michael van der Westhuizen
+1  A: 

From the pthread function prototype:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    void *(*start_routine)(void*), void *arg);

The function passed to pthread_create must have a prototype of

void* name(void *arg)
Falaina
A: 

This worked for me:

#include <iostream>
#include <pthread.h>
using namespace std;

void* print_message(void*) {

    cout << "Threading\n";
}

int main() {

    pthread_t t1;

    pthread_create(&t1, NULL, &print_message, NULL);
    cout << "Hello";

    // Optional.
    void* result;
    pthread_join(t1,&result);
    // :~

    return 0;
}
Vijay Mathew