Hi,

While compiling with Sun CC (studio10) compiler, I've observed the following warnings:

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*).

Any tips on resolving this?

Thanks,
ameli.

Dani AI

Generated

Short answer: the compiler is warning because the function pointer you passed has C++ linkage while the pthread start argument in the vendor header is declared with C linkage. As suggested, that linkage mismatch is what triggers the Anachronism warning; as asked for concrete fixes, the examples below show safe ways to resolve it.

Use a plain free function with C linkage that then calls into your C++ code. extern "C" gives the function C linkage so it matches the header and removes the warning:

extern "C" void * thread_start(void *arg) {
    MyClass *obj = static_cast<MyClass*>(arg);
    return obj->run();   // run() returns void* or convertible type
}

Do not try to make a non-static member function directly the start routine. Member functions have an implicit this and a different type; the usual pattern is the free-function wrapper above. You cannot apply extern "C" to a non-static member function, so if you want a method invoked on an object, use the wrapper to extract the pointer and call the method.

If you prefer to silence only the warning and you are certain the calling convention matches on your platform, you can cast the pointer when calling pthread_create. This hides the mismatch and should be used with caution:

pthread_create(&tid, &attr,
    reinterpret_cast<void*(*)(void*)>(thread_start),
    arg);

Caveats: casting away linkage differences can hide real ABI problems on exotic platforms; using an extern "C" wrapper is the safest, portable fix. For background on linkage and why extern "C" matters, see the language linkage reference and the pthread_create specification:

Recommended Answers

All 2 Replies

Can you post some code?

click me
My guess is you tried to pass a C++ function as a "pointer to function" which is expecting a pointer to a C function.

Perhaps some of the links will have more ideas, I didn't read them all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.