1

I am writing a program in C++ which works with threads. When I try to create a process I get the following error: Member function must be called or its address taken in function. Here is my code:

void PCB::createProcess(){
    asm cli
    this->stack = new unsigned[stackSize];

    stack[stackSize-1] = 0x200;             
    stack[stackSize-2] = FP_SEG(wrapper);
    stack[stackSize-3] = FP_OFF(wrapper);
    this->ss = FP_SEG(stack+stackSize-12);
    this->sp = FP_OFF(stack+stackSize-12);

    asm sti
}


void PCB::wrapper(){

    myThread->run();

    myThread->state = TERMINATED;
}

I get the error in these two lines:

stack[stackSize-2] = FP_SEG(wrapper);
stack[stackSize-3] = FP_OFF(wrapper);

I have tried everything. I have tried (&(wrapper)), PCB::wrapper, &PCB::wrapper and nothing helps, it just gives me more errors. PCB is the name of the class.

If anyone has an idea, please help.

ivaa14
  • 138
  • 10

1 Answers1

0

FP_SEG and FP_OFF are macros that extract the segment selector and the offset, respectively, from a pointer. That's how we manipulated pointers under DOS and early versions of Windows, twenty-plus years ago. Unless you're targeting some funky system that you haven't mention, they are totally inappropriate.

Since PCB::wrapper is a member function, a pointer to it is a pointer-to-member-function. It is not an ordinary function, so the compiler is right to complain that it can't make sense out of what the code is trying to do.

These days, multithreading is done with std::thread. You don't have to do that hacky stack manipulation; just pass the pointer-to-member-function and the this pointer to the constructor for std::thread.

std::thread thr(&PCB::wrapper, this);
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • I am writing some kind of a simple kernel of an operating system. It is supposed to register interrupts from the keyboard and display the pressed key to the console. But I am only at the beginning. I actually must use those macros, since we are using the '92. standard for C++, and such compiler. – ivaa14 Jan 11 '18 at 17:54
  • 1
    Then you **must say so** in your question. And there is no '92 standard for C++ -- the first C++ standard was 1998. Regardless: a pointer-to-member-function is not a pointer, and you can't treat it like one. Write a normal function, and call that member function on an object. – Pete Becker Jan 11 '18 at 18:49