468,121 Members | 1,528 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

Sleep function and threads

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

Nov 17 '05 #1
10 1680
> 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

Nov 17 '05 #2


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)


Nov 17 '05 #3
> 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)

Nov 17 '05 #4
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().
Nov 17 '05 #5


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.

Nov 17 '05 #6
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
Nov 17 '05 #7


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.

Nov 17 '05 #8
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(...);
Nov 17 '05 #9


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

Nov 17 '05 #10
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

Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mathias Mamsch | last post: by
29 posts views Thread by Jeffrey Maitland | last post: by
5 posts views Thread by Parahat Melayev | last post: by
8 posts views Thread by Cider123 | last post: by
2 posts views Thread by [Yosi] | last post: by
5 posts views Thread by Sinan Nalkaya | last post: by
reply views Thread by Tim Golden | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.