Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > sun 上关于 pthread_create 的 extern "C" 警告的讨论
【标  题】:sun 上关于 pthread_create 的 extern "C" 警告的讨论
【关键字】:sun,pthread_create,extern
【来  源】:http://blog.csdn.net/ablo_zhou/archive/2007/04/16/1565895.aspx

sun 上关于 pthread_create 的 extern "C" 警告的讨论

Your Ad Here

Solaris 简单程序:

 

#include <pthread.h>
#include 
<stdio.h>

/* thread function */
void* start_routine(void* arg)
{
        printf(
"thread %d running... ",*(int*)arg);
        
return NULL;
}


int main()
{
        pthread_t tid1;
        
int arg = 1;
        
int ret;
        ret 
= pthread_create(&tid1,NULL,start_routine,(void*)&arg);
        
return ret;
}

调用 CC thread1.cpp,

警告(记时错误): 正在将 void*(*)(void*) 传递给调用 pthread_create(unsigned*, const _pthread_attr*, extern "C" void*(*)(void*), void*) 时使用的类型为 extern "C" void*(*)(void*) 的形式参数 3. 

有人认为是sun的bug,有人认为不是。

解决办法

在线程函数start_routine前增加extern "C"

Warning (Anachronism)   
Nov 19, 2000 5:24 AM

Click to email this message

 
 
Hi all,

I'm getting the following compiler warning in certain cases:

Warning (Anachronism): Formal
argument 3 of type extern "C" void*(*)(void*) in call to
pthread_create(unsigned*, const _pthread_attr*,
extern "C" void*(*)(void*), void*) is being passed void*(*)(void*).

This happens when the C function I'm calling (in this case pthread_create()), expects a function as a parameter. The function I am passing it is a normal global function. The warning is coming from me not declaring my function as extern "C". What are the consequences of this warning? I've never had any problems ignoring it, but maybe there could be some... Am I safe to ignore?

Thanks!
 
jrpratt
Posts:1
Registered: 6/5/97
Warning (Anachronism)   
Dec 28, 2000 12:37 PM (reply 1 of 12)

Click to email this message

 
Not sure what the consequences are but I've run into the same warnings. There is a section in the C++ Migration guide that specifically addresses this issue. I recommend looking at the migration guide for more information.
 
flahdiri
Posts:1
Registered: 2/3/99
Re: Warning (Anachronism)   
May 15, 2002 9:32 AM (reply 2 of 12)

Click to email this message

 
Hi,

Has anyone figured this problem out ? I am getting the same warning (using C++ 5.0 and pthread.h).
Please post answers.

Thanks


--
Farid


> Hi all,
>
> I'm getting the following compiler warning in certain
> cases:
>
> Warning (Anachronism): Formal
> argument 3 of type extern "C" void*(*)(void*) in
> n call to
> pthread_create(unsigned*, const _pthread_attr*,
> extern "C" void*(*)(void*), void*) is being passed
> d void*(*)(void*).
>
> This happens when the C function I'm calling (in this
> case pthread_create()), expects a function as a
> parameter. The function I am passing it is a normal
> global function. The warning is coming from me not
> declaring my function as extern "C". What are the
> consequences of this warning? I've never had any
> problems ignoring it, but maybe there could be some...
> Am I safe to ignore?
>
> Thanks!

 
AnwerAbbas
Posts:17
Registered: 1/30/01
Re: Warning (Anachronism)   
May 15, 2002 9:30 PM (reply 3 of 12)

Click to email this message

 
Its always a good idea to remove all the warnings from c/c++ program. I treat them as errors.
Declare the function, you are passing as an argument to pthread_create, as extern "C" fn();
And it will take care of your warnings.
Recently I have done this in upgrading the code from C++ 4.2 to 5.3.
Anwer.
 
rick_campbell
Posts:1
Registered: 12/10/99
Re: Warning (Anachronism)   
May 23, 2002 1:17 PM (reply 4 of 12)

Click to email this message

 
> Its always a good idea to remove all the warnings from
> c/c++ program. I treat them as errors.
> Declare the function, you are passing as an argument
> to pthread_create, as extern "C" fn();
> And it will take care of your warnings.
> Recently I have done this in upgrading the code from
> C++ 4.2 to 5.3.
> Anwer.

It is perfectly alright to define a static function and use it as a functional parameter. The SparcWorks compiler is incorrectly confusing linkage (extern "C") with type (void* (*) (void*)) amd issuing a bogus warning.

The code is correct, the compiler is wrong.

 
joshwalker
Posts:60
Registered: 12/4/01
Re: Warning (Anachronism)   
May 23, 2002 1:55 PM (reply 5 of 12)

Click to email this message

 
Actually, I think the compiler is correct. Look at the C++ standard, last sentence of 7.5/1:

<quote>
Two function types with different language linkages are distinct types even if they are otherwise identical.
</quote>

This clearly says that language linkage is part of the type. This makes sense, because functions in different languages might use different conventions for parameter passing or be otherwise incompatible.

The safe and portable thing to do is declare the function you pass to pthread_create as extern "C".

Josh Walker


 
RobbieRAltera
Posts:1
Registered: 5/24/02
Re: Warning (Anachronism)   
May 24, 2002 11:37 AM (reply 6 of 12)

Click to email this message

 
We should be able to turn these anachronistic warnings off. Why doesn't -w disable these warnings. I would prefer it if Sun would let developers decide which warnings can be ignored and which ones should not. Don't force these warnings down our throats.

 
joshwalker
Posts:60
Registered: 12/4/01
Re: Warning (Anachronism)   
May 24, 2002 12:04 PM (reply 7 of 12)

Click to email this message

 
I agree that it is important to be able to disable warnings you don't want to see.

But note that the code causing the warning is anachronistic, not the warning ;)

Josh
 
LYLeung
Posts:3
Registered: 5/24/02
Re: Warning (Anachronism)   
May 24, 2002 2:55 PM (reply 8 of 12)

Click to email this message

 
I also ran into this problem but even after declaring the called function as extern "C", I am still getting the warning. Using the posted code as an example, I did the following in the corresonding header file:

extern "C" pthread_create (.....)

But the warning continues. Did I miss something here?

Thanks.

Lance
 
AnwerAbbas
Posts:17
Registered: 1/30/01
Re: Warning (Anachronism)   
May 29, 2002 11:20 PM (reply 9 of 12)

Click to email this message

 
You do not have to declare ptherad_create as extern "C".
The function in the argument of pthread_create needs to be declare as extern C.
Example.
extern "C" myFunction(........)
void main()
{
ptherad_create(..., &myFunction,...);

}

Hopefully it will help.


 
singhaia
Posts:2
Registered: 11/6/01
Re: Warning (Anachronism)   
Jun 7, 2002 8:26 AM (reply 10 of 12)

Click to email this message

 
Hi everyone!!!

I have spent some time banging my head for such warnings. Defining functions as extern "C" solves most of the problems. But in case of a static function being passed as a parameter which accepts and extern "C" argument the issue can be a bit tricky ..Earlier, all static fuctions in C++ had extern "C" linkage so no question of warnings..but now static functions dont have extern C linkage so we are stuck with these warnings.
 
joshwalker
Posts:60
Registered: 12/4/01
Re: Warning (Anachronism)   
Jun 7, 2002 11:15 AM (reply 11 of 12)

Click to email this message

 
Somehow I doubt that static functions ever had extern "C" linkage. Functions declared extern "C" cannot be overloaded, so this would be pretty limiting. Perhaps the compiler just didn't issue a warning before.

Josh

 
edwin_chen
Posts:1
Registered: 7/12/02
Re: Warning (Anachronism)   
Jul 12, 2002 2:47 PM (reply 12 of 12)

Click to email this message

 
I worked around the 'passing static methods' issue by adding the following lines:


typedef void* (*CVoidFunction)(void*); // extern "C++" linkage

extern "C" int CreateThread( pthread_t*& pThread, CVoidFunction pVoidFunction, void* pArgument )
{
// Create the thread
const int nResult = pthread_create( pThread,
(const pthread_attr_t*)0,
(void*(*)(void*))pVoidFunction, // cast to extern "C" void* (*)( void* )
pArgument );

return nResult;
}


It will then be possible to create threads with static methods and avoid compiler warnings!

Ubuntu与USB产业:【上一篇】
Linux系统文件命令精通指南:【下一篇】
【相关文章】
  • Oracle9i for SUN Solaris installation.
  • Apache vs Sun
  • must be an instance of com.sun错误的解决
  • 测试extern变量、常量、普通函数
  • 禁用sun 480内存插槽
  • extern的小结
  • Oracle External Table
  • extern "C"的含义
  • 解决unresolved external symbol "public: int __thiscall CWnd::KillTimer(unsigned int)" (?KillTimer@CW...
  • SUN服务器上安装solaris9文档
  • 【随机文章】
  • MPLS在帧中继网上的应用
  • 在PB中实现分段打印功能
  • 设置APACHE禁止用户浏览目录内容
  • 软件项目规模估计方法介绍
  • C++ Builder 创建分布式应用程序
  • 票据置换
  • 浅析COleDispatchDriver Class(Ole发布驱动类)
  • 什么是顶级域名
  • 利用ref游标返回结果集给客户机--C#处理示例
  • [原创]Net Framework: 字符串的驻留(String Interning)
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.