0

Hello ,

I am trying to create threads using makecontext function.

The code is sth like this..

#include "mythread.h"
#include <malloc.h>
#include <stdio.h>

#define FIBER_STACK 1024*64

int mythread_create(mythread_t *new_thread_ID,
                                        mythread_attr_t *attr,
                                        void * (*start_func)(void *),
                                        void *arg)
{
        // Get the current execution context
             mythread_t t2;
             t2 = (mythread_t)new_thread_ID;
             getcontext(&t2->context);


         // Modify the context to a new stack
         t2->context.uc_link = 0;
         t2->context.uc_stack.ss_sp = malloc( FIBER_STACK );
         t2->context.uc_stack.ss_size = FIBER_STACK;
         t2->context.uc_stack.ss_flags = 0;

        if ( t2->context.uc_stack.ss_sp == 0 )
         {
                 perror( "malloc: Could not allocate stack" );
                 return 0;
         }
         // Create the new context
         printf( "Creating child fiber\n" );
         t2->start_func = start_func;
         makecontext(&t2->context,[B][U]<--------------->[/U][/B],0);
         printf("make context");
}

void * display()
{
printf ("Hello");

}


int main()
{
   mythread_t  t1;
   mythread_create(&t1,NULL,display,NULL);
   return 0;
}

/* end of code */


my problem lies in the 2nd argument of the makecontext function as to how to pass the function pointer.(In red)

I am getting a segmentation fault too.

Please help..

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by ArkM
0

1. Please, use code tag to present your snippets. It's a nightmare to read sources with so horrible indentations w/o code tag...
2. In C a function name implicitly converted to a pointer to this function so you may simply write a function name in this position.
3. As far as I know the 2nd argument of makecontext function (from ucontext.h) has void (*)() type (a pointer to a function w/o parameters returned void). I can't see any such function in your snippet...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.