【发布时间】:2014-05-19 17:09:16
【问题描述】:
我有一个接受字符串的函数。由于 pthread 不接受 string ,因此我将函数的参数设置为 char 指针。现在我想用 pthread_create 调用该函数,但我做不到。我认为由于 void * 而出现问题。我搜索了它并进行了一些铸造,但我无法成功。我该如何修复它以便它可以在 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);
}
错误是: thread.cpp:在函数“int main()”中: thread.cpp:27:69:错误:从“void ()(char)”到“void* ()(void)”的无效转换 [-fpermissive] /usr/include/pthread.h:225:12: 错误:初始化参数 3'int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)' [ -fpermissive]
【问题讨论】:
标签: c++ multithreading