Hi,
I need help witht he sleep function as follows. I need to be write som
code to do the ff:
1. creates a new thread
2. (in the new thread), Sleep for x milliseconds
3. (in the new thread), After time elapsed, call a function via a callback
4. ability to kill the spawned thread.
Pseudocode (from the available docs) is as ff:
//Forward declarations
routine() //sleep is called in here
routine_callback() //something to do when times up
void foo( void ) {
if ((h =CreateThread( ..,routine,routine_callback,..&Id.)) {
....
}
.......
//Kill thread
CloseHandle(h) ;
}
I would like a working example that shows me how I can:
1) Specify my routine function
2). Pass a callback function and the sleep time amount to my routine
function
3) Kill the spawned thread.
Many thanks 10 1872
> 1) Specify my routine function
Something like :-
DWORD WINAPI routine( LPVOID lpParameter)
{
//...
} 2). Pass a callback function and the sleep time amount to my routine function
CreateThread has an LPVOID lpParameter. Pass a pointer to your callback
function as lpParameter.And in your thread proc (routine defined above), you
cast lpParameter back to the function pointer type of your call back
function.
3) Kill the spawned thread.
Just return from the thread proc (in this case, routine defined above)
--
Regards,
Nish [VC++ MVP] http://www.voidnish.com http://blog.voidnish.com
"Alfonso Morra" <sw***********@the-ring.com> wrote in message
news:db**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com... Hi,
I need help witht he sleep function as follows. I need to be write som code to do the ff:
1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread), After time elapsed, call a function via a callback 4. ability to kill the spawned thread.
Pseudocode (from the available docs) is as ff:
//Forward declarations routine() //sleep is called in here routine_callback() //something to do when times up
void foo( void ) { if ((h =CreateThread( ..,routine,routine_callback,..&Id.)) { .... } ....... //Kill thread CloseHandle(h) ; }
I would like a working example that shows me how I can:
1) Specify my routine function 2). Pass a callback function and the sleep time amount to my routine function 3) Kill the spawned thread.
Many thanks
Nishant Sivakumar wrote: 1) Specify my routine function
Something like :-
DWORD WINAPI routine( LPVOID lpParameter) { //... }
2). Pass a callback function and the sleep time amount to my routine function
CreateThread has an LPVOID lpParameter. Pass a pointer to your callback function as lpParameter.And in your thread proc (routine defined above), you cast lpParameter back to the function pointer type of your call back function.
I also need to pass the sleep time. It looks like the CreateProcess API
only expects one parameter to be passed. Does this mean that I have to
wrap both my callback function and sleep time in some kind of ptr to
(user defined parameter) struct or something similar ? 3) Kill the spawned thread.
I want to be able to make the decision to kill the thread from the
Parent thread. Is there anyway to "send messages" (I don't maan WM_*
messages) to the thread?. This will be necesary so that if I move this
code to class fo example, the thread can be killed in the destructor of
the parent that spawned the thread. Just return from the thread proc (in this case, routine defined above)
> I also need to pass the sleep time. It looks like the CreateProcess API only expects one parameter to be passed. Does this mean that I have to wrap both my callback function and sleep time in some kind of ptr to (user defined parameter) struct or something similar ?
Yep, you can use a struct like :-
struct THREADDATA
{
FUNC_PTR fCallBack;
DWORD dwSlepTime;
};
I want to be able to make the decision to kill the thread from the Parent thread. Is there anyway to "send messages" (I don't maan WM_* messages) to the thread?. This will be necesary so that if I move this code to class fo example, the thread can be killed in the destructor of the parent that spawned the thread.
Look up PostThreadMessage on MSDN.
--
Regards,
Nish [VC++ MVP] http://www.voidnish.com http://blog.voidnish.com
"Alfonso Morra" <sw***********@the-ring.com> wrote in message
news:db**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
Nishant Sivakumar wrote:
1) Specify my routine function
Something like :-
DWORD WINAPI routine( LPVOID lpParameter) { //... }
2). Pass a callback function and the sleep time amount to my routine function
CreateThread has an LPVOID lpParameter. Pass a pointer to your callback function as lpParameter.And in your thread proc (routine defined above), you cast lpParameter back to the function pointer type of your call back function.
I also need to pass the sleep time. It looks like the CreateProcess API only expects one parameter to be passed. Does this mean that I have to wrap both my callback function and sleep time in some kind of ptr to (user defined parameter) struct or something similar ?
3) Kill the spawned thread.
I want to be able to make the decision to kill the thread from the Parent thread. Is there anyway to "send messages" (I don't maan WM_* messages) to the thread?. This will be necesary so that if I move this code to class fo example, the thread can be killed in the destructor of the parent that spawned the thread.
Just return from the thread proc (in this case, routine defined above)
Nishant Sivakumar wrote: I also need to pass the sleep time. It looks like the CreateProcess API only expects one parameter to be passed. Does this mean that I have to wrap both my callback function and sleep time in some kind of ptr to (user defined parameter) struct or something similar ?
Yep, you can use a struct like :-
struct THREADDATA { FUNC_PTR fCallBack; DWORD dwSlepTime; };
Third member would be a HANDLE to the non-signaled manual reset event
that parent thread sets when it's time for the new thread to finish.
This struct should be created on the heap, so that it still exists when
the new thread begins execution. New thread can delete the struct when
it finishes the execution.
Instead of CreateThread() (not CreateProcess) you may have to use
_beginthreadex().
Mihajlo Cvetanović wrote: Nishant Sivakumar wrote:
I also need to pass the sleep time. It looks like the CreateProcess API only expects one parameter to be passed. Does this mean that I have to wrap both my callback function and sleep time in some kind of ptr to (user defined parameter) struct or something similar ?
Yep, you can use a struct like :-
struct THREADDATA { FUNC_PTR fCallBack; DWORD dwSlepTime; };
Third member would be a HANDLE to the non-signaled manual reset event that parent thread sets when it's time for the new thread to finish.
I'm intrigued, please tell me more. How would I set this handle and use
it. i.e. what would I initialize it to and how would I actually use it
inb the code? (a snippet would be useful) This struct should be created on the heap, so that it still exists when the new thread begins execution. New thread can delete the struct when it finishes the execution.
How may I explicitly create a struct on the heap (you're not taling
about global variable declaration here are you?) Instead of CreateThread() (not CreateProcess) you may have to use _beginthreadex().
Again, a snippet that actually shows how to do this, would be very
useful and much appreciated.
Alfonso Morra wrote: Hi,
I need help witht he sleep function as follows. I need to be write som code to do the ff:
1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread), After time elapsed, call a function via a callback 4. ability to kill the spawned thread.
Something like this:
#include <process.h>
#include <windows.h>
#include <stdio.h>
struct SleeperThreadData
{
DWORD timeout;
HANDLE endEvent;
void (*callback)(void* param);
void* paramValue;
};
unsigned int __stdcall SleeperThreadFunc(void* param)
{
SleeperThreadData* data = (SleeperThreadData*)param;
if (WaitForSingleObject(data->endEvent, data->timeout) == WAIT_TIMEOUT)
{
//timed out!
(data->callback)(data->paramValue);
_endthreadex(0);
}
return 1;
}
void callback(void* param)
{
char const* s = (char*) param;
puts(s);
fflush(stdout);
}
int main()
{
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
SleeperThreadData threadData = {
2000,
event,
callback,
"Hello World!"
};
unsigned threadID;
HANDLE threadHandle = (HANDLE) _beginthreadex(NULL, 0,
SleeperThreadFunc, &threadData, 0, &threadID);
WaitForSingleObject(threadHandle, INFINITE);
CloseHandle(threadHandle);
threadHandle = (HANDLE) _beginthreadex(NULL, 0, SleeperThreadFunc,
&threadData, 0, &threadID);
Sleep(200);
//fire the event to cancel timeout
SetEvent(event);
puts("Cancelled!");
fflush(stdout);
//wait for thread exit
WaitForSingleObject(threadHandle, INFINITE);
puts("Thread exited!");
fflush(stdout);
//clean up.
CloseHandle(threadHandle);
CloseHandle(event);
}
Obviously, using a nice threading library like boost.threads would make
the code cleaner. In any case, it should all be wrapped into a nice
little library that takes care of the clean up of parameters, etc.
Tom
Tom Widmer [VC++ MVP] wrote: Alfonso Morra wrote:
Hi,
I need help witht he sleep function as follows. I need to be write som code to do the ff:
1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread), After time elapsed, call a function via a callback 4. ability to kill the spawned thread.
Something like this:
#include <process.h> #include <windows.h> #include <stdio.h>
struct SleeperThreadData { DWORD timeout; HANDLE endEvent; void (*callback)(void* param); void* paramValue; };
unsigned int __stdcall SleeperThreadFunc(void* param) { SleeperThreadData* data = (SleeperThreadData*)param; if (WaitForSingleObject(data->endEvent, data->timeout) == WAIT_TIMEOUT) { //timed out! (data->callback)(data->paramValue); _endthreadex(0); } return 1; }
void callback(void* param) { char const* s = (char*) param; puts(s); fflush(stdout); }
int main() { HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); SleeperThreadData threadData = { 2000, event, callback, "Hello World!" }; unsigned threadID; HANDLE threadHandle = (HANDLE) _beginthreadex(NULL, 0, SleeperThreadFunc, &threadData, 0, &threadID); WaitForSingleObject(threadHandle, INFINITE); CloseHandle(threadHandle);
threadHandle = (HANDLE) _beginthreadex(NULL, 0, SleeperThreadFunc, &threadData, 0, &threadID);
Sleep(200); //fire the event to cancel timeout SetEvent(event); puts("Cancelled!"); fflush(stdout); //wait for thread exit WaitForSingleObject(threadHandle, INFINITE); puts("Thread exited!"); fflush(stdout); //clean up. CloseHandle(threadHandle); CloseHandle(event); }
Obviously, using a nice threading library like boost.threads would make the code cleaner. In any case, it should all be wrapped into a nice little library that takes care of the clean up of parameters, etc.
Tom
Hi Tom, this is trully wonderful code you have provided. Something I can
really get my teeth stuck into and build on from there. I did want to
use the Boost libararies (because of my cross platform needs) but there
is a section where it (Boost.Threads) mentions potentially non-thread
safe functions - and ctime functions are right in the middle of it. For
now, I will simply expand on the example you kindly provided - as a
proof of concept. If I get stuck, I'll likely come in here again to ask
for more help. Many thanks once again Tom.
Tom Widmer [VC++ MVP] wrote: HANDLE threadHandle = (HANDLE) _beginthreadex(NULL, 0, SleeperThreadFunc, &threadData, 0, &threadID); WaitForSingleObject(threadHandle, INFINITE); CloseHandle(threadHandle);
This is a redundancy, I believe. Instead of these 4 lines there should
be only:
HANDLE threadHandle;
In this example both thread creation and thread abortion are in the same
function, so SleeperThreadData can be created locally. If it were the
case where they are in different functions then this struct should have
been created on the heap:
SleeperThreadData* pThreadData = new SleepThreadData(...);
Alfonso Morra a écrit : Mihajlo Cvetanović wrote:
Nishant Sivakumar wrote:
I also need to pass the sleep time. It looks like the CreateProcess API only expects one parameter to be passed. Does this mean that I have to wrap both my callback function and sleep time in some kind of ptr to (user defined parameter) struct or something similar ?
Yep, you can use a struct like :-
struct THREADDATA { FUNC_PTR fCallBack; DWORD dwSlepTime; };
Third member would be a HANDLE to the non-signaled manual reset event that parent thread sets when it's time for the new thread to finish.
I'm intrigued, please tell me more. How would I set this handle and use it. i.e. what would I initialize it to and how would I actually use it inb the code? (a snippet would be useful)
struct THREADDATA
{
FUNC_PTR fCallBack;
DWORD dwSlepTime;
HANDLE hTerminateEvent;
};
THREADDATA data;
data.hTerminateEvent=::CreateEvent(NULL, TRUE, FALSE, NULL); //manual
reset, non signalled event.
See documentation for SetEvent and WaitForSingleObject to learn how to
use an event. (in your thread, you should use WaitForSingleObject
instead of sleep). This struct should be created on the heap, so that it still exists when the new thread begins execution. New thread can delete the struct when it finishes the execution.
How may I explicitly create a struct on the heap (you're not taling about global variable declaration here are you?)
THREADDATA* data=new THREADDATA();
//initialize *data fields
//pass data as parameter to CreateThread
At the end of the worker thread, do not forget to delete data. Instead of CreateThread() (not CreateProcess) you may have to use _beginthreadex().
Again, a snippet that actually shows how to do this, would be very useful and much appreciated.
RTFM! Look the documentaion for _beginthreadex in MSDN : there is an
example right on the page!
Arnaud
MVP - VC
Mihajlo Cvetanović wrote: Tom Widmer [VC++ MVP] wrote: HANDLE threadHandle = (HANDLE) _beginthreadex(NULL, 0, SleeperThreadFunc, &threadData, 0, &threadID); WaitForSingleObject(threadHandle, INFINITE); CloseHandle(threadHandle);
This is a redundancy, I believe. Instead of these 4 lines there should be only:
HANDLE threadHandle;
Tom was simply illustrating points 3 and 4 in the OPs question - waiting for
the thead to timeout and killing the thread.
-cd This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mathias Mamsch |
last post by:
Hi all, I got a problem with writing a python extension module which
launches a bunch of C threads (currently under windows). The module launches
a complete speech recognition engine in a new...
|
by: Jeffrey Maitland |
last post by:
Hello all,
I am in the process of writing a multithreading program and what I was
wondering is a sleep command in an executing function will affect the
threads below it? Here is a basic example...
|
by: Parahat Melayev |
last post by:
I am trying to writa a multi-client & multi-threaded TCP server.
There is a thread pool. Each thread in the pool will handle requests
of multiple clients.
But here I have a problem. I find a...
|
by: Cider123 |
last post by:
I ran into a situation where my Window Service had to process 100,000+
files, when I first noticed I needed to tweak various routines.
Everything runs fine, but here's what I ran into:
In the...
|
by: [Yosi] |
last post by:
I have C# application, in main form I made thead...
User click on susspend button which cause to suspend the thread, in the
button fuction:
mThread.Susspend();
What I should do to wait in this...
|
by: kiplring |
last post by:
Suppose a function which has Sleep() method in it. And I want to
recycle it. I made two buttons which call "Resume()" and "Suspend()".
But It doesn't work. The state of thread "t" will be...
|
by: Sinan Nalkaya |
last post by:
hello,
i need a function like that,
wait 5 seconds:
(during wait) do the function
but function waits for keyboard input so if you dont enter any it waits
forever.
i tried time.sleep() but when...
|
by: OlafMeding |
last post by:
Below are 2 files that isolate the problem. Note, both programs hang
(stop responding) with hyper-threading turned on (a BIOS setting), but
work as expected with hyper-threading turned off.
...
|
by: Tim Golden |
last post by:
Lowell Alleman wrote:
Well you've certainly picked a ticklish area to run
into problems with ;). First, forget about the
threading aspects for the moment. AFAICT the smallest
program which...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |