-
August 20th, 2013, 06:02 AM
#1
[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?
-
August 20th, 2013, 06:33 AM
#2
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 20th, 2013, 06:46 AM
#3
Re: why multithread class don't accept the precedure name?:(
Originally Posted by 2kaud
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
thanks for all
-
August 20th, 2013, 08:35 AM
#4
Re: why multithread class don't accept the precedure name?:(
Could you explain what these three lines of your code are supposed to do?
Originally Posted by Cambalinho
Code:
Handle_Of_Thread_1=0;
CloseHandle(Handle_Of_Thread_1);
TerminateThread( Handle_Of_Thread_1,1);
Victor Nijegorodov
-
August 20th, 2013, 08:53 AM
#5
Re: why multithread class don't accept the precedure name?:(
Originally Posted by VictorN
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
-
August 20th, 2013, 09:11 AM
#6
-
August 20th, 2013, 10:00 AM
#7
Re: why multithread class don't accept the precedure name?:(
Originally Posted by VictorN
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)
-
August 20th, 2013, 10:29 AM
#8
Re: why multithread class don't accept the precedure name?:(
Originally Posted by Cambalinho
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
-
August 20th, 2013, 10:31 AM
#9
Re: why multithread class don't accept the precedure name?:(
Originally Posted by Cambalinho
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 20th, 2013, 10:45 AM
#10
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?
-
August 20th, 2013, 11:41 AM
#11
Re: [RESOLVED] why multithread class don't accept the precedure name?:(
Originally Posted by Cambalinho
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
-
August 20th, 2013, 11:44 AM
#12
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 20th, 2013, 11:56 AM
#13
Re: [RESOLVED] why multithread class don't accept the precedure name?:(
Originally Posted by 2kaud
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?
-
August 26th, 2013, 06:45 AM
#14
Re: [RESOLVED] why multithread class don't accept the precedure name?:(
Originally Posted by Cambalinho
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|