Sleep function and threads

  • Thread starter Thread starter Alfonso Morra
  • Start date Start date
A

Alfonso Morra

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
 
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)
 
Nishant said:
Something like :-

DWORD WINAPI routine( LPVOID lpParameter)
{
//...
}




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 ?
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.
 
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.
 
Nishant said:
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 said:
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 said:
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 said:
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 said:
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 :
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).
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.
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 said:
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
 
Back
Top