Solved

_beginthread problems

Posted on 1997-02-05
1
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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