[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Classes + Threads

I had a fully working multi-threaded windows application.
Unfortunately, I had to convert it to be Object Oriented,
and now I have problems with my CreateThread calls.
I had some module and I wrapped all it's functions
in a class, and I want to get the same functionality.
But when I call CreateThread from one of the methods
to create a new thread with some oter method, I get next error:
cannot convert parameter 3 from 'unsigned long (__stdcall *(void *))(void *)' to 'unsigned long (__stdcall *)(void *)
All this worked before both functions were inserted into the class. Any suggestions?

P.S.
Called function declared as:
DWORD WINAPI class_name::function_name(LPVOID lpvThreadParam);
i.e.

Thanks in advance
0
finik
Asked:
finik
  • 4
  • 3
1 Solution
 
chensuCommented:
Define it as a static member function. A non-static member function has an implicit parameter, which is the this pointer. Therefore, the function prototype does not match the one required by CreateThread.
0
 
chensuCommented:
If you are going to call C run-time routines from a program built with LIBCMT.LIB, you must start your threads with the _beginthread function. Do not use the Win32 functions ExitThread and CreateThread.
0
 
finikAuthor Commented:
1. I tried the static function, but static couldn't access any no static functions and variables. I tried this->function_name as well, but is seems to lead to the same result as w/out the this->.

2. What's wrong about CreateThread and ExitThread?
What c run-time routines are we talking about? I use ExitThread a lot in my program.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
finikAuthor Commented:
Sorry, the error is:
cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)'
and not the one stated in the question.
0
 
chensuCommented:
1. You may pass the this pointer as a parameter of the thread controlling function.

2. Any C run-time routines, such as strcpy.

C Run-Time Library Functions for Thread Control
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vccore/_core_c_run.2d.time_library_functions_for_thread_control.htm

Library Support for Multithreading
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vccore/_core_library_support_for_multithreading.htm
0
 
finikAuthor Commented:
Thanks, I'll through a look at the links.
But it seems, like my problems are much bigger, and not as technical as it looked in the question in the first place. My program was totally multithreaded. It was a socks proxy/firewall,
and there was like socks_listener, a lot of socks_threads(every client gets one per connection, http_listener, a lot of http_threads (same thing)..and so on. Now, when I was told to rewrite it OOP way, I thought of the whole! http module as an object. But, as I understand now from what I've seen, it couldn't be 1 object and split into different threads (according to methods http::http_listener and http::http_thread). Am I right? Like I saw in MSDN, if you want to create a thread in MFC/OOP environment, you need a controlling function and to provide an object as a parameter. But that means that I have to split to smaller
objects like (http_listener, http_thread). But then it doesn't make sence either, since I get a full program of small objects, each one of them with only 1 function (I can even make it a constructor). Am I missing something in OOP concept?
0
 
chensuCommented:
You don't have to split it. You may add some (static or non-static) member variables to indicate the status. These will be passed along with the object. Since I don't see the entire view of your project, it is hard for me to give any useful suggestion. Sorry.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now