Sleep Thread

Hey, I'm trying to use a timer but not to be the main thread because I want the program I'm detouring to go on running and do not have to wait untill Sleep period ends and then continue, so here is my source code.. everything works fine but it's just that my Timer affects the main thread and everything stops untill sleep period ends, there u go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int __fastcall MyWeaponPutOff(void *Item, void *edx, int Player)
{
       
        int Time = 0;
        for(;;)
        {
                if( Time >= Timer )
                {
                        if ( IsNormal(Player) ) // if player online it turns to original function
                        return WeaponPutOff(Item,Player);
                        Time = 0;
                }
                Sleep(1000);
                Time++;
        }
       
                return 0;
}
 


Thanks in advance!
sleep(...) stops whatever thread activated it. If MyWeaponPutOff was called in the main thread, then it will stop for 1000 mseconds
then can you please show me how to make sleep without letting the main thread affected?
I even tried to make sub functions but I still don't get it...

If someone could apply to my code how to make Sleep without affecting the main thread would be great..
I literally cant sleep at night because of this one lol :D

Thanks in advance =)
I even tried to make sub functions but I still don't get it...
You should launch another thread. Not just call function. Look into links I provided. There is examples here.
yep been trying but I get tons of errors everytime I try to pass variables into it, it does not want to accept it... so if someone knows how could u show me on my example please it would be more understandable for me,, thanks
multithreading is a heavy subject, basically you can think of making a new thread like making a new main, then the new threads runs on different code then your original main. If there is sleep in the code that the main runs on, then main will sleep. But if the sleep is in another threads code, then that thread will sleep. I personally dont understand why you need multithreading.

what you probably want to do is not use sleep at all. But make a function that will return after 1000 seconds, and meanwhile it will try to do some work. for that you need some sort of class called JobPool that you give it jobs to do. when you activate it, it will perfore a job and check what time it is, whether its time to stop doing jobs or not. then take another job, do job.run(), then see what time it is again, all that in a loop, that looks something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class JobPool{

   Queue<Job> job_queue;
   Job * job;

   activate(int time){
 
       while( time >= 0 ){
          timer.start();
          *job = job_queue.pop();
          (*job).run();
          time -= timer.get_ticks();
   }

   assign(Job job);


}
Last edited on
1. Which headers do I need to include, I've gave it a guess of including vector/map but activate, Queue still gives me an error

2. Does this work same way as Sleep, but here time >= 0 represents seconds?

Thanks so much for taking the time and patience to answer me
Alright, I've tried that one still relies on the Main Thread which is not what I have been seeking so...
Example of threaded asynchronous timer: http://www.cplusplus.com/forum/beginner/102780/
Topic archived. No new replies allowed.