- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个接受字符串的函数。由于 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: 错误:正在初始化 'int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)' 的参数 3 [ -f宽容]
最佳答案
pthread_create
期望的参数是,
void *(*)(void *)
这是一个指向接受空指针并返回空指针的函数的指针。
将您的方法更改为具有以下签名:
/*static*/ void* printString(void *x) { ... }
关于c++ - 使用接受 char 指针的函数调用 pthread_create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912735/
我正在努力实现以下目标: 强制新创建的线程在 pthread_create() 之后立即开始运行。没有使用实时调度。 来自pthread_create() man page : Unless real
我想做这样的事情: void *do_work_son(void *data) { mystruct *d = (mystruct*)data; while(tr
typedef struct client { pthread thread; Window_t *win }client; client * client_create(int ID)
我编译最新的 buildroot,并使用输出主机 mipsel-linux-gcc 来编译我的 c 程序。我已经测试了 hello world 程序,它在 MIPS 机器上运行良好(实际上是一个用 p
每当我在我的程序上运行 valgrind 时,它都会告诉我在调用 pthread_create 的任何地方都可能丢失了内存。我一直在尝试遵循 上的指导 valgrind memory leak err
是否有(在 glibc-2.5 和更新版本中)为 pthread_create 定义 Hook 的方法? 有很多二进制应用程序,我想编写一个动态库以通过 LD_PRELOAD 加载 我可以在 main
我正在开发一个多线程程序,但是由于某种原因,我无法创建线程。当我尝试调试时,它在我的pthread_join语句处中断。 for (i = 0; i < numThreads; ++i) { pt
我使用pthread_create()创建了一个线程。新线程创建成功,控制权传递给新创建的线程。然而,主线程似乎不再执行了。主线程处于无限循环中并且永远不会退出。以下是代码片段: void *star
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Multiple arguments to function called by pthread_creat
我在 GCC 中运行我的程序时遇到了段错误。这是一个相当长的程序,所以我只发布我认为相关的部分;如果需要更多信息,请告诉我。 void *RelaxThread(void *para) { p
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
所以我之前就一个具体问题提出了一个问题。我已经查看了该网站上的其他问题,但大多数问题都没有解决我的问题,特别是尽管我认为这个问题对其他初学者很有用。这是代码。 (pi.h) 我的结构是如何布局的 #i
我是 C 新手,这些指针的概念对我来说非常困惑。我试图做一些看起来很简单的事情,但我遇到了很多编译错误。 我想生成一个新线程并将多个指针作为参数传递给它(似乎在 c 中使用全局变量的唯一方法是通过指针
我一生都无法弄清楚为什么这是段错误。 这是段错误 get_ranks_parallel 上线 for (struct node* node = data->plist->head; node!=NUL
我的服务器正在向客户端发送 udp 数据报,并在数据包丢失时从客户端接收 NACK 数据报。我想创建一个线程来处理每个 NACK 数据包,但只有当我有东西要从客户端接收时我才想创建线程。为此,我想使用
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我看过pthread_create的文档 在底部的示例中,他们使用的是: pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &t
我需要将多个参数传递给我想在单独线程上调用的函数。我有read执行此操作的典型方法是定义一个结构体,向函数传递一个指向该结构体的指针,并为参数取消引用它。但是,我无法让它工作: #include #
所以我试图编写一个程序,在c中创建一个线程,其工作是找到2个给定数字中的最大值。我编写的第一个程序(名为askisi.c)如下: #include #include #include int m
程序中存在两个问题首先,当我在主函数中取消注释 pthread_join() 时,会出现段错误,否则程序将运行...其次,输出文件将丢失上次读取文件中存储在全局变量words中的每个单词的第一个字母。
我是一名优秀的程序员,十分优秀!