Solved

_beginthread problems

Posted on 1997-02-05
1
543 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now