[RESOLVED] why multithread class don't accept the precedure name?:(
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: [RESOLVED] why multithread class don't accept the precedure name?:(

  1. #1
    Join Date
    Apr 2009
    Posts
    705

    [RESOLVED] why multithread class don't accept the precedure name?:(

    heres my Multihtread class:
    Code:
    class MultiThread{
    	private:		
    		bool blnDestroyed;
    		HANDLE Handle_Of_Thread_1;
    		typedef void (*MyThreadProc)();
    		MyThreadProc ThreadProcedure;
    		static DWORD WINAPI StaticThreadProc(LPVOID param) 
    		{
    			static_cast<MultiThread*>(param)->MultithreadProcedure();
    			return 0;
    		}
    
    
    		void   MultithreadProcedure() 
    		{
    			for (;;) 
    			{
    				if (blnDestroyed==true) break;			
    				ThreadProcedure();
    			}
    			CloseHandle(Handle_Of_Thread_1);
    			TerminateThread( Handle_Of_Thread_1,1);
    		}
    
    
    	public:
    		int Start(MyThreadProc ThreadProc) 
    		{
    			ThreadProcedure = ThreadProc;
    			Handle_Of_Thread_1=0;
    			CloseHandle(Handle_Of_Thread_1);
    			TerminateThread( Handle_Of_Thread_1,1);
    			blnDestroyed=false;
    			HANDLE hMyThread;
    			hMyThread = CreateThread(NULL, 0, StaticThreadProc, this, 0, NULL);
    			return 0;
    		}
    
    
    		int Stop()
    		{
    			blnDestroyed=true;			
    			return 0;
    		}
    
    
    		MultiThread::~MultiThread() //for stop if the process isn't stoped before be destroyed
    		{
    			Stop();
    		}
    };
    heres how i use it:
    Code:
    #include "stdafx.h"#include "Console.h"
    #include "Thread.h"
    
    
    Console a;
    
    
    int  show()
    {
    	for(;;)
    	{
    		cout << "hi";
    		Sleep(1000);
    		a.Clear();
    	}	
    	return 0;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	MultiThread b;
    	b.Start(show()); 	  
        a.Read();	
        return 0;
    }
    error message:
    1 - "error C2664: 'MultiThread::Start' : cannot convert parameter 1 from 'int' to 'MultiThread::MyThreadProc'";
    2 - " IntelliSense: argument of type "int" is incompatible with parameter of type "MultiThread::MyThreadProc"".
    what i'm wrong?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,723

    Re: why multithread class don't accept the precedure name?:(

    You're got two problems with this code

    1) show is defined as returning an int, but MyThreadProc is defined as returning void - so either show has to return void or MyThreadProc has to be defined as returning an int (I changed show to return void for my test).

    2) when you pass a pointer to a function (as passing it to b.Start), you just pass the function name

    Code:
    b.Start(show);
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2009
    Posts
    705

    Re: why multithread class don't accept the precedure name?:(

    Quote Originally Posted by 2kaud View Post
    You're got two problems with this code

    1) show is defined as returning an int, but MyThreadProc is defined as returning void - so either show has to return void or MyThreadProc has to be defined as returning an int (I changed show to return void for my test).

    2) when you pass a pointer to a function (as passing it to b.Start), you just pass the function name

    Code:
    b.Start(show);
    thanks for all

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: why multithread class don't accept the precedure name?:(

    Could you explain what these three lines of your code are supposed to do?
    Quote Originally Posted by Cambalinho View Post
    Code:
    			Handle_Of_Thread_1=0;
    			CloseHandle(Handle_Of_Thread_1);
    			TerminateThread( Handle_Of_Thread_1,1);
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2009
    Posts
    705

    Re: why multithread class don't accept the precedure name?:(

    Quote Originally Posted by VictorN View Post
    Could you explain what these three lines of your code are supposed to do?
    if 1 thread(these thread) is used, then terminate it and create another one

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: why multithread class don't accept the precedure name?:(

    Quote Originally Posted by Cambalinho View Post
    if 1 thread(these thread) is used, then terminate it and create another one
    But how could you CloseHandle while handle is already NULL?
    And how could you use TerminateThread passing in the invlid handle (it is either NULL or, at least not more valid after CloseHandle was called!)?
    Besides, using TerminateThread to exit the thread is a very very wrong (or at least very poor) design. Again: read MSDN about TerminateThread and how to exit the worker thread.
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 2009
    Posts
    705

    Re: why multithread class don't accept the precedure name?:(

    Quote Originally Posted by VictorN View Post
    But how could you CloseHandle while handle is already NULL?
    And how could you use TerminateThread passing in the invlid handle (it is either NULL or, at least not more valid after CloseHandle was called!)?
    Besides, using TerminateThread to exit the thread is a very very wrong (or at least very poor) design. Again: read MSDN about TerminateThread and how to exit the worker thread.
    by reading msdn information the TerminateThread is very 'dangerous'. but if is, how can i terminate a thread?
    (i will see in other pages, maybe i can find something)

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: why multithread class don't accept the precedure name?:(

    Quote Originally Posted by Cambalinho View Post
    by reading msdn information the TerminateThread is very 'dangerous'. but if is, how can i terminate a thread?
    (i will see in other pages, maybe i can find something)
    Thead exits itself returning some value (exit code) from its thread procedure.
    To signal a thread to exit from outside the thread some Event is usually set (and thread must regularly check if the event was set or not using some of the Wait functions)
    Victor Nijegorodov

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,723

    Re: why multithread class don't accept the precedure name?:(

    Quote Originally Posted by Cambalinho View Post
    by reading msdn information the TerminateThread is very 'dangerous'. but if is, how can i terminate a thread?
    (i will see in other pages, maybe i can find something)
    You don't force terminate a thread (except in dire emergencies!). A thread functions ends itself by returning normally. If one thread wants control over another then this is accomplished via messages, events etc. If you force terminate a thread then object destructors are not called, data isn't flushed to disk etc etc. When you start dealing with multiple threads you need to have knowledge of multi-tasking and how things are done. Just because something can be done using the available api, this doesn't mean they should be done!

    I suggest you read Windows via c/c++ by Jeffrey Richter
    http://www.amazon.co.uk/Windows-Via-...+via+c+c%2B%2B

    Also, you shouldn't use CreateThread. You should use _beginthreadex(...)
    http://msdn.microsoft.com/en-us/libr...vs.100%29.aspx

    _beginthreadex correctly sets up the c/c++ run-time library variables for multithreaded environments (eg errno, strerror, asctime etc etc) whereas CreateThread does not.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 2009
    Posts
    705

    Re: [RESOLVED] why multithread class don't accept the precedure name?:(

    see these 1 or 2 changes in my class. and it's commented in most important parts:
    Code:
    #include <Windows.h>
    
    class MultiThread
    {
    	//private:		
    		bool blnDestroyed;
    		HANDLE Handle_Of_Thread_1;
    		typedef void (*MyThreadProc)();
    		MyThreadProc ThreadProcedure;
    		static DWORD WINAPI StaticThreadProc(LPVOID param) 
    		{
    			static_cast<MultiThread*>(param)->MultithreadProcedure();
    			return 0;
    		}
    
    		void   MultithreadProcedure() 
    		{
    			//see these infinite loop. have 1 variable for exit			
    			for (;;) 
    			{
    				if (blnDestroyed==true) break;			
    				ThreadProcedure();
    			}	
    
    			//then terminate the thread	 
    			CloseHandle(Handle_Of_Thread_1);
    			TerminateThread( Handle_Of_Thread_1,1);
    		}
    
    	public:
    		int Start(MyThreadProc ThreadProc) // i did a change;)
    		{
    			blnDestroyed=true;
    			ThreadProcedure = ThreadProc;
    			Handle_Of_Thread_1=0;						
    			HANDLE hMyThread;
    			blnDestroyed=false;
    			hMyThread = CreateThread(NULL, 0, StaticThreadProc, this, 0, NULL);			
    			return 0;
    		}
    
    		int Stop()
    		{
    			blnDestroyed=true;			
    			return 0;
    		}
    
    		~MultiThread()
    		{
    			Stop();
    		}
    };
    like you see, before terminate the thread i use a variable inside of cycle for and see the Start() function too
    i must ask: continues incorrect?

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: [RESOLVED] why multithread class don't accept the precedure name?:(

    Quote Originally Posted by Cambalinho View Post
    see these 1 or 2 changes in my class. and it's commented in most important parts:
    Code:
    			...
    			//then terminate the thread	 
    			CloseHandle(Handle_Of_Thread_1);
    			TerminateThread( Handle_Of_Thread_1,1);
    			...
    like you see, before terminate the thread i use a variable inside of cycle for and see the Start() function too
    i must ask: continues incorrect?
    You either did not read the post#6 or you've just ignored it.
    Why?
    Victor Nijegorodov

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,723

    Re: [RESOLVED] why multithread class don't accept the precedure name?:(

    No. I don't think that you have been following the advice offered on previous threads and understand multi-processing

    1) Don't use CreateThread() - use _beginthreadex().

    2) Don't use TerminateThread - use messages and events.

    3) In MultithreadProcedure you are repeatly calling ThreadProcedure in an infinite loop until a flag is set. You wouldn't normally spin like this as it uses resources. You would use messages/events.

    4) Handle_of_thread_1 is set to 0 and then used as a parameter to CloseHandle and TerminateThread??? As a mimimum, Handle_of_thread_1 should be set to hMyThread after the CreateThread function (but see 1) above).

    5) You don't test that CreateThread has succeeded or failed.

    6) You set bLnDestroyed to true at the start of start function and then a few lines later set it to false without being used inbetween.

    7) blnDestroyed is set in one thread and tested in another. The compiler doesn't know about multi-tasking and might easily optimse the test in multithread procedure so that it loads the value once into a register and never looks at the actual value again. So blnDestroyed could be set to true yet the test still fail. This is where criticalsections and the interlocked family of functions come in. As a minumum, blnDestroyed should be defined as volatile.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Apr 2009
    Posts
    705

    Re: [RESOLVED] why multithread class don't accept the precedure name?:(

    Quote Originally Posted by 2kaud View Post
    No. I don't think that you have been following the advice offered on previous threads and understand multi-processing

    1) Don't use CreateThread() - use _beginthreadex().

    2) Don't use TerminateThread - use messages and events.

    3) In MultithreadProcedure you are repeatly calling ThreadProcedure in an infinite loop until a flag is set. You wouldn't normally spin like this as it uses resources. You would use messages/events.

    4) Handle_of_thread_1 is set to 0 and then used as a parameter to CloseHandle and TerminateThread??? As a mimimum, Handle_of_thread_1 should be set to hMyThread after the CreateThread function (but see 1) above).

    5) You don't test that CreateThread has succeeded or failed.

    6) You set bLnDestroyed to true at the start of start function and then a few lines later set it to false without being used inbetween.

    7) blnDestroyed is set in one thread and tested in another. The compiler doesn't know about multi-tasking and might easily optimse the test in multithread procedure so that it loads the value once into a register and never looks at the actual value again. So blnDestroyed could be set to true yet the test still fail. This is where criticalsections and the interlocked family of functions come in. As a minumum, blnDestroyed should be defined as volatile.
    sorry the _beginthreadex don't needs be terminated or something?

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,043

    Re: [RESOLVED] why multithread class don't accept the precedure name?:(

    Quote Originally Posted by Cambalinho View Post
    sorry the _beginthreadex don't needs be terminated or something?
    Something, it's always something. Something that you need to learn by reading trustworthy sources like MSDN and books written by recognized technical writers. E.g. Richter, or Johnson M. Hart. There you'll find the answers why not to use TerminateThread, or what is the difference between bare CreateThread and runtime _beginthreadex, as well as lots of other details. But by no means you should learn complex technical topics by just asking on public forums.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center