Yes, that adapter will work, although it is not a genuine Pentax adapter. Some on this board insist that only the genuine Pentax brand adapter will do. I have had good luck with third-party adapters.
I have bought an adapter for every M42 lens that I use on my dslr. I remove the spring clip on the adapter. This means that the adapter will not lock onto the camera, but, in my experience, friction has been more than enough to hold it on the camera.
There is another type of adapter, like this one:
NEW M42 Lens TO PK Pentax FOR Pentax KM K M K 7 K X K2000 K20D Adapter Ring | eBay
These adapters have a large flange that sits outside the lens mount. This prevents the lens from focusing to unfinity. This is fine if you only want to use the lens for macro shots. For normal use, where infinity focus is needed, it is worthless. The proper adapter sits entirely within the lens mount. No part of the adapter extends forward of the camera lens mount.
As for those thrift store lenses. Be careful, not all M42 lenses were made by Pentax. The M42 mount is sometimes referred to as the "Universal Mount", because many camera makers used that mount. As someone else said, not many Pentax lenses (Super Takumar or Super MultiCoated Takumar) were truly bad. Just some were better than others. The same can not be said of third party lenses. Even though they will work on your Pentax dslr, many are not worth the time and effort.