473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

using threads with C++

arunmib
104 100+
Hi all,
Following is my program,

My header file,
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. class Test
  4. {
  5.    private:
  6.         ........ some declarations......
  7.     public:
  8.        Test();
  9.        ~Test();
  10.  
  11.      int AnotherFunc();
  12.  
  13.       //my thread function
  14.       static DWORD WINAPI threadFunc(LPVOID);
  15. }
  16.  
my CPP file
Expand|Select|Wrap|Line Numbers
  1.  
  2. int iGlobal = 0;
  3.  
  4. Test::Test()
  5. {
  6.    ......some processes and declarations....
  7.    HANDLE hThrd = CreatThread(NULL, 0, threadFunc, NULL, 0, NULL);
  8.    .........some other processes.......
  9.    CloseHandle(hThrd);
  10. }
  11.  
  12. int Test::AnotherFunc()
  13. {
  14.   ........
  15.   return 0;
  16. }
  17.  
  18. DWORD WINAPI Test::threadFunc(LPVOID lpArgs)
  19. {
  20.     iGlobal = 0;
  21.     .....it does some processs......
  22.     if(iGlobal == 1)
  23.        AnotherFunc();
  24.  
  25.     return 0;
  26. }
  27.  
I am using borland C++ builder 6 and my OS is WinXP. When I build this project I get the error, "Use . or -> to call Test::AnotherFunc()". I am not able to understand why this is happening ?

If I try to declare iGlobal in my Test class, I get the error message "member Test::iGlobal cannot be used without an object".

Can somebody help me with these two errors and explain these errors....
May 25 '08 #1
2 1698
weaknessforcats
9,208 Expert Mod 8TB
Your THREADPROC is a static function, as it should be.

However, as a static function it can't call a class member function since there is no object. That is, the static function does not have a this pointer.

What you should do is pass the this pointer to your THREADPROC as the LPVOID argument. Then the THTREADPPROC can typecast the LPVOID back to your class type and you can go from there. However, you can't do this in a constructor because the Test object is not yet initialized.

Instead, have a Launch() method to call the CreateThread and an Execute() that can be called by the thread:

Expand|Select|Wrap|Line Numbers
  1. class Test
  2. {
  3.       public:
  4.          DWORD Launch();
  5.          DWORD Execute();
  6.          static DWORD WINAPI threadfunc(LPVOID arg);
  7.  
  8. };
  9.  
  10.  
  11.  
  12. Test::Launch()
  13. {
  14.    ......some processes and declarations....
  15.    HANDLE hThrd = CreatThread(NULL, 0, threadFunc, this, 0, &hThrd);
  16.    if (!hThrd)
  17.    {
  18.                 //CreateThread failed. GetLastError()
  19.    }
  20.    .........some other processes.......
  21.    CloseHandle(hThrd);
  22. }
  23.  
The threadfunc knows the the LPVOID is really a Test* so it is safe to typecast the LPVOID to a Test*:
Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI Test::threadfunc(LPVOID arg)
  2. {
  3.     reinterpret_cast<Test*> (arg)->Execute();
  4.     return 0;   //thread has completed
  5. }
  6.  
It is Test::Excute that contains the thread logic. This function should be called only by the THREADPROC. When it returns, the threadfunc returns and your thread is complete. This Test::Execute() is where you can WaitForSingleObject. Since Test::Execute() is a member function, it has the this pointer which means you can call any other class method from this function.

In main() it looks like:

Expand|Select|Wrap|Line Numbers
  1. Test obj;
  2.  
  3. DWORD rval = obj.Launch();
  4. //Thread is complete here
  5.  
May 25 '08 #2
arunmib
104 100+
thanks weaknessforcats, that has made things clear for me.... :-)
May 25 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Przemysław Różycki | last post by:
Hello, I have written some code, which creates many threads for each connection ('main connection'). The purpose of this code is to balance the load between several connections ('pipes'). The...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
4
by: Lucas Tam | last post by:
Is there a document online which details the advantages of using a Threadpool? My application uses a user configuration amount of threads which does the following: Main Thread Gives Work to...
4
by: Mrinal Kamboj | last post by:
Hi , I had a doubt regarding a piece of code with me , that has to do with System.Threading.Thread class . In it user instantiates an array of Thread class and to all of them assign a method...
5
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2...
4
by: gsimmons | last post by:
I've been researching multi-threaded WinForms apps and thread synchronization stuff for a couple days since I'm working on refactoring a multi-threaded GUI app at work and want to be sure it's...
6
by: Gina_Marano | last post by:
Hey All, I am using multiple child threads per main thread to download files. It sometimes appears as if the same file is being downloaded twice. I am using "lock". Am I using it correctly? Any...
6
by: Ricardo Vazquez | last post by:
Hello everybody, I'm programming a TCP server. During the stress tests I noticed that maybe my socket-receiving thread became deaf after an hour of heavy stress. I think that the reason could...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
3
by: Steven Blair | last post by:
I have been watching an MSDN video on the PFX Task class and have a question. Here is my scenario: TcpListener waits on incoming connections. Once a new connection is established, a new Task...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.