c++ - C 与 C++ 中的 Pthread 之间的差异

标签 c++ c pthreads posix ada

所以我能够使用 Ada_function'Address 将 Ada 函数传递给 C_function。这是 C 函数:

void Create_Process(int * status, void * function) {
    pthread_t new_thread;

    //creating function with the given function and no arguments.
    *status = pthread_create(&new_thread, NULL, function, NULL);
}

这工作得很好。我的问题是当我尝试在 C++ 中使用相同的函数时。编译失败并出现错误:

error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ [-fpermissive]
*status = pthread_create(&new_thread, NULL, function, NULL);

有什么理由可以在 C 而不是 C++ 中运行/编译?

最佳答案

C++ 中的隐式类型转换比 C 中严格得多。 void * function 参数不能用作 C++ 或 C 中的函数指针...

您的函数原型(prototype)中需要 void* (*function)(void*)

关于c++ - C 与 C++ 中的 Pthread 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881052/

相关文章:

c - 如何将 10 位值传递给两个寄存器?

c++ - synergy 1.4.16 编译失败 - 未定义对 `pthread_xxxx' 的引用

C: 如何使用 POSIX 线程声明递归互斥体?

c++ - 将 ostringstream 输出到文件

c++ - x86暂停指令的跨平台实现

c++ - strtok 不返回 nullptr

c - 奇怪的多线程行为

c++ - WinAPI 将 FILETIME 格式化为带有日期和/或时间的本地化字符串

python - 为什么在给定的 ctypes 结构打包示例中 sizeof 会有所不同?

java 转换整数以通过网络发送