Solved

_beginthread problems

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert MFC APP to Win32 APP. 19 109
C++ help/ Toy problem 19 49
find Speed using GPS latitude & longitude 4 80
How do I get Window Title of all opened process? 4 32
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

685 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