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

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值