Creating a function like Sleep()

Just wondering if it's possible to make my own function that does the same as sleep. To wait for a certain amount of time and to kill the thread?

If anyone already has one made and could show me how they made theirs or what not would be greatly appreciated.

Cheers guys,
Myth.
What OS is this for?
In the reference section there is a C sample for something like that: http://www.cplusplus.com/reference/clibrary/ctime/clock.html
That function is not suitable for applications requiring real time response. If there are as many or more threads inside its loop than there are CPUs in the system, there could be a serious performance downgrade.
You can actually create your own function by

creating your own header file.

just name your header file and define everything you

want.

as far as the compiler can read it .
I haven't done this in a long time. I'm actually sorry I have to do it again.

............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\

Sometimes it's like people want you to hate them.
closed account (S6k9GNh0)
:$ ^ lawlz

Look at the boost library. I think they have something in there similar to a cross platform delay function that you may be able to take and edit. Don't take my word for it though...
I asked about the OS because WIN32 and pthreads can wait with timeouts. If you're waiting for a thread you don't need a seperate timer.
Using Windows Media Center Edition ;)

@Bazzy - " http://www.cplusplus.com/reference/clibrary/ctime/clock.html " Doesn't kill a thread?

The program this is for is just using one thread. I'm not multi threading with this program at the moment. I just really hate the sleep() function.

@computerquip - I'll have a look at the boost library now if i have it.

and

@helios - You always manage to crack me up haha :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
                                            ________
                                    , -‘”                   ``~ ,
                             , -”                                   “- ,
                         ,/                                               ”:,
                     ,?                                                      \,
                   /                                                           ,}
                 /                                                      ,:`^`  }
               /                                                   ,:”         /
              ?     __                                         :`           /
             /__ (     “~-,_                              ,:`          /
           /(_    ”~,_        “~,_                    ,:`        _/
          {  _$;_      ”=,_       “-,_       , -~-,}, ~”;/    }
           ((     *~_       ”=- _      “;,, /`    /”              /
   ,,,___ \`~,      “~ ,                    `     }              /
            (    `=-,,       `                        (      ;_,,-”
            / `~,      `-                               \      /\
             \`~ *-,                                     |, /     \,__
,,_          } >- _\                                   |              `=~-,
     `=~-,_\_      `\,                                 \
                   `=~-,, \,                               \
                                `:,,                           `\              __
                                     `=-,                   ,%`>--==``
                                        _\           _,-%       `\
                                   ,<`  _|_,-&``                `\

== win
Last edited on
I've tried that with simple (Win API) multithreading and it worked as I expexted
(I've tried with Sleep too but it didn't work)
Last edited on
Using the clock as we talked about in another thread works but. It doesn't kill the thread. So the cpu itself is being hogged by the program for me. When I was doing it with the clock I was getting 50% of the cpu used. With my timer functions to check the ms it takes to execute the code. Then adding a sleep with the appropriate values I need. I can get the cpu to 5 - 15%.
There is a lot more code than this but heres is the code I'm using. I know there are more ways to do it but. This is going by a small assignments guidelines.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
static LONGLONG s_TimerTicksPerSecond = 0;
static LONGLONG s_TimerTicksPerMillisecond = 0;
static LONGLONG s_TimerStart = 0;
static LONGLONG s_TimerStop = 0;

void TimerInitialise();
void TimerStart();
void TimerStop();
int TimerElapsedMS();

void main()
{
	TimerInitialise();

	//Create Stuff here...
	//Populate map / create struct/characters

	TimerInitialise();

	do
	{
		TimerStart();

		//All of the games code goes here

		do
		{
			TimerStop();
		} while (TimerElapsedMS() < 1);

		Sleep(150 - TimerElapsedMS());

		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
	}while( gameRunning == true );
}

//Timer Functions
void TimerInitialise()
{
	// Returns ticks per second
	QueryPerformanceFrequency( (LARGE_INTEGER*) &s_TimerTicksPerSecond);

	s_TimerTicksPerMillisecond = s_TimerTicksPerSecond / 1000;
}

void TimerStart()
{
	QueryPerformanceCounter( (LARGE_INTEGER*) &s_TimerStart);
}

void TimerStop()
{
	QueryPerformanceCounter( (LARGE_INTEGER*) &s_TimerStop);
}

// Returns the time elapsed between TimerStart and TimerStop, measured in Milliseconds
int TimerElapsedMS()
{
	LONGLONG      t;

	t = s_TimerStop - s_TimerStart;
	t /= s_TimerTicksPerMillisecond;

	return (int) t;
}		


Theres more to this for safety checking and what not. But heres the basic example of what I wanted without using sleep.
Topic archived. No new replies allowed.