Solved

_beginthread problems

Posted on 1997-02-05
1
540 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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 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.

762 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

10 Experts available now in Live!

Get 1:1 Help Now