-
It is possible?
Hello guys. I have a very big question for you...i can't find any solution.
Let's say that i have this code:
void check()
{
while(true)
{
cout<<"blablabla";
}
}
int main()
{
check();
}
So it is an infinite loop. The question is:
It is possible to stop the function while it is executing without using any other logical var like
while(x==1)
{
do something;
}
I'm thinking at something like stop(*check) or...i don't know. I was wondering... if i could keep into a pointer the adress of that check() call it would be possible that by changing its value with null i mean *ptr=null to stop the function?
Why i need it? I'm doing a sockets listening function so i need to stop the
while(true){
if(sock=accept(.....))
list.add(sock);
} through an another Stop function
Conclusion: I don't want to use a logical variable to stop the function while it is executing. I want to close it at any time with another function... It would be possible?
Thank you and sorry for my poor English!
-
You need to read this before posting: http://cboard.cprogramming.com/c-pro...ead-first.html. Then, make sure you use code tags when you post.
As for your question, standard C does not allow that. Standard C doesn't let you have a second function executing simultaneously to change a variable in the first function.
You can get around this general problem using threads, but they're not portable, i.e. threads work differently on Windows, Linux, etc, and it's a cheap hack of a solution for your problem, IMO.
As for your socket code, you can use non-blocking sockets. I don't know if you're using Windows or Linux, but their sockets libraries are very similar, so check out this networking guide: Beej's Guide to Network Programming.