pthread_create问题

lockonh 2014-04-11 05:17:52
我想把一个保存字符串的string变量传给pthread_create的第4个参数,报错显示无法把string转换成void *,我后来改了用字符数组来保存字符串结果就可以,不知道还有没有其他实现的方法。
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lockonh 2014-04-11
  • 打赏
  • 举报
回复
引用 4 楼 luotuo44 的回复:
[quote=引用 3 楼 lockonh 的回复:] [quote=引用 1 楼 luotuo44 的回复:] 取这个string变量的地址即可。

std::string str;
pthread_create(&tid, NULL, threadfunc, &str);
在threadfunc里面,再把参数强制转换成string类型即可。
我试了下,pthread_create那一行没报错,但是在threadfunc中参数强制转换成string类型时
cout<<"this is one thread : "<<(string)arg<<endl;
报错了 invalid conversion from 'void*' to 'const char*'[/quote] 转成string类型的指针。std::string* str = (std::string*)(arg);[/quote] 果然可以,赞一个
luotuo44 2014-04-11
  • 打赏
  • 举报
回复
引用 3 楼 lockonh 的回复:
[quote=引用 1 楼 luotuo44 的回复:] 取这个string变量的地址即可。

std::string str;
pthread_create(&tid, NULL, threadfunc, &str);
在threadfunc里面,再把参数强制转换成string类型即可。
我试了下,pthread_create那一行没报错,但是在threadfunc中参数强制转换成string类型时
cout<<"this is one thread : "<<(string)arg<<endl;
报错了 invalid conversion from 'void*' to 'const char*'[/quote] 转成string类型的指针。std::string* str = (std::string*)(arg);
lockonh 2014-04-11
  • 打赏
  • 举报
回复
引用 1 楼 luotuo44 的回复:
取这个string变量的地址即可。

std::string str;
pthread_create(&tid, NULL, threadfunc, &str);
在threadfunc里面,再把参数强制转换成string类型即可。
我试了下,pthread_create那一行没报错,但是在threadfunc中参数强制转换成string类型时
cout<<"this is one thread : "<<(string)arg<<endl;
报错了 invalid conversion from 'void*' to 'const char*'
luotuo44 2014-04-11
  • 打赏
  • 举报
回复
取这个string变量的地址即可。

std::string str;
pthread_create(&tid, NULL, threadfunc, &str);
在threadfunc里面,再把参数强制转换成string类型即可。

65,207

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧