linux 多线程编程中 pthread_create编译出错
来源:互联网 发布:人工智能 基金 编辑:程序博客网 时间:2024/05/16 14:20
小程序实例
#include <stdio.h>
#include <pthread.h>
void thread(void*ptr)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread./n");
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if(ret!=0){
printf ("Create pthread error!/n");
exit (1);
}
for(i=0;i<3;i++)
printf("This is the main process./n");
pthread_join(id,NULL);
return (0);
}
g++ 编译出现错误 gcc ThreadMain.c -lpthread -o ThreadMain
错误如下:
invalid conversion from `void*' to `void*(*)(void*)
解决分析:
建立线程函数的原型:
int pthread_create (pthread_t *__restrict __threadp,
__const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROW;
这个call中的第三个参数是载入一个函数,这个函数有一个参数可以传入,返回一个通用指针。
我们再来看看原来函数中是怎样调用这一原型的,基本上就是类似一下的调用方式:
(void *)&main_thread
这个表达式的含义:取一个指向函数main_thread的指针,然后将其转换为一个通用指针。
值得注意的是在gcc编译时不会出错,但是用g++就会有问题,究其原因就是C语言编译器允许隐含性的将一个通用指针转换为任意类型的指针,而C++不允许。
- linux 多线程编程中 pthread_create编译出错
- Linux多线程编译中为什么说pthread_create未定义
- linux多线程编程(pthread_join/pthread_create)
- linux多线程之pthread_create
- pthread_create 多线程-linux
- Linux多线程函数pthread_create()函数
- linux-多线程设计pthread_create、…
- Linux下clang/gcc使用pthread.h编译出错undefined reference to `pthread_create'
- linux编程之pthread_create函数
- linux编程之pthread_create函数
- linux编程之pthread_create函数
- linux下编译多线程程序, undefined reference to `pthread_create',undefined reference to `pthread_join'
- 转:多线程编程之pthread_create函数应用
- 多线程编程之pthread_create函数应用
- 多线程编程之pthread_create函数应用
- 多线程编程之pthread_create pthread_join pthread_attr_t
- 多线程编程之pthread_create函数应用
- 多线程编程中遇到的问题——undefined reference to 'pthread_create'
- 并查集学习
- SAP BW学习第一课:BW建模——总账练习操作步骤记录
- php_mysql
- QT的文件传输
- 这是一个关于ruby on rails的博客测试 test
- linux 多线程编程中 pthread_create编译出错
- Linux I2C驱动完全分析(一)
- MFC消息响应
- js去掉数组中的重复元素
- poj 1088 基础的DP
- 进程线程同步 进程通信
- 计算机生成随机数的一种方法
- poj 3348 凸包
- 数据库简单命令