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 9190345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:26:25+00:00 2026-06-17T20:26:25+00:00

As a part of an assignment, I am trying to create a user level

  • 0

As a part of an assignment, I am trying to create a user level thread library like pthreads.

For handling the context switching between the threads, I am using ‘swapcontext’ function. Before using it I have to make a context using ‘makecontext’ function. ‘makecontext’ expects a function pointer with return type void and argument type (void).

Whereas, the thread function has to be of the type void* thread_func (void*)

Is there a way to do a typecasting? Or is there some other way to do context switching at the user level?

  • 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-06-17T20:26:26+00:00Added an answer on June 17, 2026 at 8:26 pm

    It is illegal to invoke a function with an incompatible prototype by casting the address of the function to a different prototype and invoking it through the resulting pointer:

    void *my_callback(void *arg) { ... }
    
    void (*broken)(void *) = (void (*)(void *)) my_callback;
    broken(some_arg);   // incorrect, my_callback returns a `void *`
    

    What you can do is pass to makecontext your own callback which will call thread_func and ignore its return value. A small function that only serves to call another function is sometimes called a trampoline.

    /* return type is compatible with the prototype of the callback received
       by makecontext; simply calls the real callback */
    static void trampoline(int cb, int arg)
    {
      void *(*real_cb)(void *) = (void *(*)(void *)) cb;
      void *real_arg = arg;
      real_cb(real_arg);
    }
    
    int my_pthread_create(void *(*cb)(void *), void *arg)
    {
      ucontext_t *ucp;
      ...
      /* For brevity treating `void *` as the same size as `int` -
         DO NOT USE AS-IS.
         makecontext exposes an annoyingly inconvenient API that only
         accepts int arguments; correct code would deconstruct each
         pointer into two ints (on architectures where pointer is
         larger than int) and reconstruct them in the trampoline. */
      makecontext(ucp, trampoline, 2, (int) cb, (int) arg);
      ...
    }
    

    For bonus points, you can modify the trampoline to store the void * value returned by the callback function on the stack and have your equivalent of pthread_join() retrieve it.

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

Sidebar

Related Questions

as a part of a homework assignment, I'm trying to read a single char
I'm doing the SaaS Stanford class, trying to do Part 5 of this assignment
I'm trying to complete the last part of assignment 5 of cs193p ( link
I'm trying to parse user-inputted prefix notation logical expressions using a context-free-grammar with the
As part of a python assignment I'm trying to implement list sorting (ascending) without
As part of an assignment, I need to read data from a binary file
Hi I am new to Python and as a part of my assignment I
So I'm having a little trouble with the final part of my assignment on
As part of the last assignment in a beginner python programing class, I have
As part of a homework assignment in Lisp, I am to use apply or

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.