1

I have a function which takes string. Since pthread don't accept string , i make the function's parameter char pointer. Now I want to call that function with pthread_create but i can not do it. Problem arise because of void * i think. I searched it and make some casting but i cannot succeed. How can i fix it so that it can work under g++

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

#define NUM_THREADS     5

void printString(char *x)
{
    cout << x << endl;
        pthread_exit(NULL);
}

int main ()
{
     pthread_t threads[NUM_THREADS];
     int rc;
     int i;
     string temp = "hello";

  char *bufferG;
  bufferG = new char[temp.size() + 1]; 
  std::copy(temp.begin(), temp.end(), bufferG); 
  bufferG[temp.size()] = '\0'; 

     for( i=0; i < NUM_THREADS; i++ ){
         cout << "main() : creating thread, " << i << endl;
         rc = pthread_create(&threads[i], NULL, printString,  &bufferG ); //(void *) &bufferG also doesn't work
     }
 pthread_exit(NULL);
 }

the error is : thread.cpp: In function ‘int main()’: thread.cpp:27:69: error: invalid conversion from ‘void ()(char)’ to ‘void* ()(void)’ [-fpermissive] /usr/include/pthread.h:225:12: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)’ [-fpermissive]

2 Answers 2

3

The argument expected by pthread_create is,

void *(*)(void *)

that is a pointer to function accepting a void pointer and returning a void pointer.

Change your method to have the following signature:

/*static*/ void* printString(void *x) { ... }
Sign up to request clarification or add additional context in comments.

2 Comments

it can compile but cannot print "hello" whether cout << *x or cout << x
@user1308990 Don't pass &bufferG but rather bufferG in your pthread_create.
1

Ok, try this out, now the main thread joins on children to execute and also instead of printing single character within thread fn, we are now using the whole buffer instead

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

#define NUM_THREADS     5

void * printString(void *x)
{
    cout << ((char *)x)<< endl;
}

int main ()
{
     pthread_t threads[NUM_THREADS];
     int rc;
     int i;
     string temp = "hello";

  char *bufferG;
  bufferG = new char[temp.size() + 1]; 
  std::copy(temp.begin(), temp.end(), bufferG); 
  bufferG[temp.size()] = '\0'; 


     for( i=0; i < NUM_THREADS; i++ ){
         cout << "main() : creating thread, " << i << endl;
         rc = pthread_create(&threads[i], NULL, printString,  bufferG ); 
     }
    for( i=0; i < NUM_THREADS; i++ ){
     pthread_join(threads[i], NULL);
    }
 pthread_exit(NULL);
 }

4 Comments

can be compiled but cannot print "HELLO"
@user1308990, try the edited anser now, should work as intended
@user1308990, if the answer is acceptable, you may please accept for my two cents
two answer cannot be accepted simultaneously and since @mockinterface answered first it would be unfair for him

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.