When you're working on problems from textbooks, you know that the problem has a solution, because the author claims that it has.
But when you're doing original research, you generally don't know whether what you're looking for exists. You might spend weeks on it, but there might simply not be a solution at all.
How do you decide to give up on such a problem?
That's impossible for anyone else to know. Just look at the 350 year long history of Fermat's last theorem. Until the mid 1990's, no one knew whether it was true (and not for lack of trying). If Andrew Wiles hadn't succeeded in the end, he would've spent almost a decade locked up in his own attic for nothing. He didn't give up. Should he have? In all likelihood, many would have advised him to give up (and I'm sure some did), but that's not the same thing.
It's not about whether you objectively should give up at any given point; it's always about the risk and whether you personally think it's worth it. And you are the only one who can answer that.