Solved

_beginthread problems

Posted on 1997-02-05
1
549 Views
Last Modified: 2008-03-03
I am having problems using Visual Age 3.0 for OS/2's _beginthread function to launch a member function of a class. The linkage appears to be correct, and even though I have modified the header and cpp files for a variety of return types I continually get the following:


"Cannot apply Optlink linkage to type void"

The member declarations appear below:

The problem appears comes up in the startThreads member function. If I remove the beginthread call, the program works perfect, although the file updates are seriesed and not multi-threaded. I AM using the -GM+ compiler option.



// Feature source code generation begins here...
INotificationId ActualCompare::FileName1Id = "ActualCompare::fileName1";
INotificationId ActualCompare::FileName2Id = "ActualCompare::fileName2";

IString ActualCompare::FileName1() const
{
  return iFileName1;
}

ActualCompare& ActualCompare::setFileName1(const IString& aFileName1)
{
  if (!(iFileName1 == aFileName1))
  {
    iFileName1 = aFileName1;
    notifyObservers(INotificationEvent(ActualCompare::FileName1Id, *this));
  } // endif
  return *this;
}

IString ActualCompare::FileName2() const
{
  return iFileName2;
}

ActualCompare& ActualCompare::setFileName2(const IString& aFileName2)
{
  if (!(iFileName2 == aFileName2))
  {
    iFileName2 = aFileName2;
    notifyObservers(INotificationEvent(ActualCompare::FileName2Id, *this));
  } // endif
  return *this;
}

void _Optlink ActualCompare::readFile1(void *)
{
FILE *file1;

file1 = fopen(FileName1(),"r");

int OrgChar = 0;
int count = 0;
int buffercount = 0;
char buffer[16] = {0};

      while ((OrgChar = fgetc(file1)) !=EOF)
      {
      iListBox1->addAsLast(_itoa(count,buffer,16));
            
            for (buffercount=0; buffercount<16; buffercount++)
            {
                  if (buffer[buffercount] == '\0')
                  break;
            }// end of for loop
            
            buffer[buffercount] = 'h';
            buffer[(buffercount+1)] = '\0';
      
      iStaticText20->setText(buffer);

      iListBox8->addAsLast(_itoa(count,buffer,10));

      iListBox2->addAsLast(_itoa(OrgChar,buffer,16));

      iListBox4->addAsLast(_itoa(OrgChar,buffer,10));

      sprintf(buffer,"%c", OrgChar);

      iListBox6->addAsLast(buffer);

      count++;
      }

fclose(file1);

}

void _Optlink ActualCompare::readFile2(void *)
{
FILE *file2;

file2 = fopen(FileName2(),"r");

int OrgChar = 0;
int count = 0;
int buffercount = 0;
char buffer[16] = {0};

      while ((OrgChar = fgetc(file2)) !=EOF)
      {
      _itoa(count,buffer,16);

            for (buffercount=0; buffercount<16; buffercount++)
            {
                  if (buffer[buffercount] == '\0')
                  break;
            }// end of for loop
            
            buffer[buffercount] = 'h';
            buffer[(buffercount+1)] = '\0';
      
      iStaticText21->setText(buffer);

      iListBox3->addAsLast(_itoa(OrgChar,buffer,16));

      iListBox5->addAsLast(_itoa(OrgChar,buffer,10));

      sprintf(buffer,"%c", OrgChar);

      iListBox7->addAsLast(buffer);

      count++;

      }

fclose(file2);

}

int ActualCompare::startThreads()
{
int tid1 = 0; int tid2 = 0;

tid1 = _beginthread(readFile1,NULL,8192,NULL);
tid2 = _beginthread(readFile2,NULL,8192,NULL);

return 0;

}

// Feature source code generation ends here.
0
Comment
Question by:dcbdbis
1 Comment
 
LVL 1

Accepted Solution

by:
Suluc earned 100 total points
ID: 1162356
It's nice to see an OS/2 User ;>

There are two possible things here:
1) You didn't make the function static
2) You need to use something like:
  void APIENTRY ActualCompare::readFile2(void *);
APIENTRY will set your function to the correct linkage and is used extensively in the OS/2 header files (I *think I got the spelling correct, you may have to check pm.h/os2defs.h)

Now, I think (juding by the code inside the functions) it is more likely that you did #1 ;>

Allow me to explain, any given C++ member function might ACTUALLY look like this to the compiler:
   void ActualCompare::readFile2(void *,ActualCompare *this);
Doesn't look like the parameter beginthread expects anymore. The hidden 'this' pointer allows the compiler to gain access the the local data members and member functions of the class the function was invoked against.

Now, when you use beginthread the OS cannot pass the hidden this pointer, nor can it use the 'system' calling convetion. You must explicitly do this for yourself using the void * data pointer. I use something like:

class T
{
  static void ThreadStub(void *TheThis)
  {
    T *This = (T *)TheThis;
    This->Thread();
  }
  void Thread();  // This does the actual work.
}

Basically, whenever you want non-C++ code to call a C++ object it must be done to a static member function. As well beginthread expects the calling convention to be whatever VAC calls System (syscall, _system, System etc are common, APIENTRY is a macro for you compiler see os2defs.h). If I recall optlink is VAC's normal linking type?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question