0

I am trying to read in input from the command line using a pthread. The pthread would call a reading function. Having some trouble with this and I have read the POSIX documentation. Appreciate the help!

int main(int argc , char *argv[])
{
    pthread_t client_thread; // client thread
    int rc;
    string msg;
    cout<<"Please enter a message to send to the server: "<<endl;
    pthread_create(&client_thread, NULL, readerT, &msg);

    cout<<"Msg is: "<<msg<<endl;

    return 0;
}

void * readerT(string * temp)
{
    cout<<"GOT IN HERE:\n"<<endl;
    getline(cin,*temp);
}

Current Error Msg:

Client.cpp: In function ‘int main(int, char**)’:
Client.cpp:33: error: invalid conversion from ‘void* (*)(std::string*)’ to ‘void* (*)(void*)’
Client.cpp:33: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
5
  • 1
    join the created thread, so that the main thread will not come to an end. Commented Mar 3, 2014 at 5:15
  • i added the pthread_join(client_thread, NULL); after the cout<<"Msg is... but I get a bunch of errors with regards to invalid conversions :( Commented Mar 3, 2014 at 5:25
  • 1) Check return value of thread creation, make sure you edit the question with proper error message so that, engineers can help you with ease. Commented Mar 3, 2014 at 5:31
  • If your compiler supports c++11 you might consider using std::thread, see this example nullptr.me/2011/09/16/multithreading-in-c11-part-1/… Commented Mar 3, 2014 at 5:44
  • @Masterminder I have made few changes in my answer it should work now... check... Commented Mar 3, 2014 at 9:11

3 Answers 3

3

Just read the error message:

Client.cpp:33: error: invalid conversion from ‘void* (*)(std::string*)’ to ‘void* (*)(void*)’

The thread function has to be of type:

void * (*)(void *)

To change this

void * readerT(string * temp)

to be

void * readerT(void * temp)
Sign up to request clarification or add additional context in comments.

Comments

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

void * readerT(void* );
int main(int argc , char *argv[])
{
    pthread_t client_thread; // client thread
    int rc;
    string msg;
    cout<<"Please enter a message to send to the server: "<<endl;
    pthread_create(&client_thread, NULL, readerT, &msg);
    pthread_join(client_thread,NULL);
    cout<<"Msg is: "<<msg<<endl;

    return 0;
}

void * readerT(void * temp)
{
    string *tmp = (string*)(temp);
    getline(cin,*tmp);
}

Hope that will work...(try to analyze what was wrong with your code :-) )

7 Comments

Although you are roughly pointing to a bug, it still is not related to question at all. Moreoever, an answer should explain what is wrong and how it can be fixed (if ever).
I guess you haven't saw the original question... error was obvious and yes I havn't worked with pthread so was not aware of the syntax..
I think you should visit this before saying that .....cplusplus.com/reference/string/string/getline
@HadeS I have made your suggested changes, however I still get these errors: Client.cpp: In function ‘int main(int, char**)’: Client.cpp:33: error: invalid conversion from ‘void* ()(std::string)’ to ‘void* ()(void)’ Client.cpp:33: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)’ ... Do I have to do some kind of casting? That line 33 error refers to where the pthread_create is
@Masterminder sorry for small mistake... changes return type from void to void *.. try now..
|
0

I think there may be two problems in your code

  • callback_function of thread is wrong

    void *readerT(string *temp)

    to

    void *readerT(void *temp)

  • use join function in main thread

If you don't do it, the main thread will soon quit after creating the read_in thread, so use a join function to wait the thread to complete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.