简体   繁体   English

使用带有char指针的函数调用pthread_create

[英]calling pthread_create with a function which takes char pointer

I have a function which takes string. 我有一个需要字符串的函数。 Since pthread don't accept string , i make the function's parameter char pointer. 由于pthread不接受string,因此我将函数的参数设为char指针。 Now I want to call that function with pthread_create but i can not do it. 现在我想用pthread_create调用该函数,但是我做不到。 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++ 我如何解决它,使其可以在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] 错误是:thread.cpp:在函数'int main()'中:thread.cpp:27:69:错误:从'void( )(char )'到'void *( )(void )'的无效转换[- [fpermissive] /usr/include/pthread.h:225:12:错误:初始化'int pthread_create(pthread_t *,const pthread_attr_t *,void *( )(void ),void *)'的参数3 [-fpermissive]

The argument expected by pthread_create is, pthread_create期望的参数是,

void *(*)(void *)

that is a pointer to function accepting a void pointer and returning a void pointer . 那是一个指向函数的指针,该函数接受一个void指针并返回一个void指针

Change your method to have the following signature: 更改您的方法以具有以下签名:

/*static*/ void* printString(void *x) { ... }

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 好的,尝试一下,现在主线程在子级上联接以执行,并且除了在线程fn中打印单个字符之外,我们现在使用整个缓冲区

#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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM