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;
}
#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 | ||
|
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! |
|
Warning (Anachronism) Dec 28, 2000 12:37 PM (reply 1 of 12) | ||
|
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.
|
|
Re: Warning (Anachronism) May 15, 2002 9:32 AM (reply 2 of 12) | ||
|
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! |
|
Re: Warning (Anachronism) May 15, 2002 9:30 PM (reply 3 of 12) | ||
|
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. |
|
Re: Warning (Anachronism) May 23, 2002 1:17 PM (reply 4 of 12) | ||
|
> 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. |
|
Re: Warning (Anachronism) May 23, 2002 1:55 PM (reply 5 of 12) | ||
|
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 |
|
Re: Warning (Anachronism) May 24, 2002 11:37 AM (reply 6 of 12) | ||
|
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.
|
|
Re: Warning (Anachronism) May 24, 2002 12:04 PM (reply 7 of 12) | ||
|
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 |
|
Re: Warning (Anachronism) May 24, 2002 2:55 PM (reply 8 of 12) | ||
|
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 |
|
Re: Warning (Anachronism) May 29, 2002 11:20 PM (reply 9 of 12) | ||
|
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. |
|
Re: Warning (Anachronism) Jun 7, 2002 8:26 AM (reply 10 of 12) | ||
|
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. |
|
Re: Warning (Anachronism) Jun 7, 2002 11:15 AM (reply 11 of 12) | ||
|
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 |
|
Re: Warning (Anachronism) Jul 12, 2002 2:47 PM (reply 12 of 12) | ||
|
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! |