473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1971
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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...
29
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...
5
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...
8
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...
2
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...
3
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...
5
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...
17
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. ...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.