Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:19:53+00:00 2026-05-27T07:19:53+00:00

I want to use the thunk function template to pass it around to pthread_create

  • 0

I want to use the thunk function template to pass it around to pthread_create and stuff like that.

I would expect the compiler to instantiate the function with the given parameters, all the info is there, and then use the typedef as a function ptr to pass to those functions.

#include <string>
class ServerImpl{
    public:
        ServerImpl(std::string host, int port);
        void run();

};


template<typename T,void (T::*mem_fn)()>
void *thunk(void *obj) {
    (static_cast<T*>(obj)->*mem_fn)();
    return 0;
}

typedef void *(*Function) (void);
Function fun = (Function)&thunk<ServerImpl,&ServerImpl::run>;
fun();

JUST CHANGED IT

See, with this I get a linker error, which is what it brought me to the previous one. Omit the fact that I should be passing an object.

Server.o: In function `ServerImpl::ServerImpl(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
Server.cpp:(.text+0x11f): undefined reference to `void* thunk<ServerImpl, &(ServerImpl::run())>(void*)'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:19:53+00:00Added an answer on May 27, 2026 at 7:19 am

    thunk is a function template. How can you use typedef on it? It doesn’t make sense. It is not a type. You can apply typedef only on types.

    You should do this:

     ServerImpl *arg = new ServerImpl(); //why do I use new?
     pthread_create(&thread_id, NULL, &thunk<ServerImpl,&ServerImpl::run>, arg);
    

    Your arguments to pthread_create are wrong. It takes 4 arguments, not 2.

    Why did I use new?

    I created an instance of ServerImpl using new, because the instance has to exist even if the function in which you created the thread have returned. If I don’t create the instance using new, and instead use local variable, then the code invoke undefined behavior, if the function returned but the thread continue running.


    Reply to your edit:

    Function fun = (Function)&thunk<ServerImpl,&ServerImpl::run>;
    fun();
    

    This is wrong. Because thunk takes one argument. So you should be doing this instead:

    Function fun = (Function)&thunk<ServerImpl,&ServerImpl::run>;
    fun(new ServerImpl); //pass an argument
    

    The member function run is invoked on this argument which you passed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a transaction log file in CSV format that I want use to
I want to create a collection of classes that behave like math vectors, so
I have a project which I would like to make more use of smart
I think I want to use pythons built in calendar module to create an
I want to use google chrome as a control. I think Enso does this
I'm struggling to think of a good way to use linq2sql, simply I want
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
Below is my stored procedure. I want use stored procedure select all row of
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.