I have made screw threads before on OpenSCAD - it is very powerful. The simple way to make a screw thread is to use the linear_extrude function to extrude a slightly offset circle with a twist setting.
The end result works well enough as a screw thread. For the internal thread in a nut you simply make the circle slightly bigger to give some clearance. However there are a few issues with this.
But I thought I would have a go at making a standard thread instead. My first idea was that instead of rotating a circle I would create a 2D shape that matches the cross section of a standard thread, and linear_extrude/twist that.
That, in itself, was not easy, I ended up using a lookup function to linear interpolate the radius for each angle: an arc at the higher radius; then an arc linearly reducing to the lower radius; then an arc at the lower radius; then a final arc linearly increasing back to the higher radius. Using lookup and a loop to create a set of 2D points for the polygon function I was able to create the correct shape, and use it with linear_extrude/twist, and it looked fine and worked.
However, I realised another problem with the whole idea of using linear_extrude. It comes down to how it works. It makes a number of slices (you can say how many) and each slice takes the 2D shape, and then a small increment above it, and twisted accordingly, the same shape, and it creates the sides to join them together, like this.
The issue is when the twist between two slices is large, like this, and each of the side triangles ends up stretched across a chord which cuts off the edge of the circle (or whatever) creating this sort of squeezed middle between the layers.
So I started again. I created a set of 3D points of the helix that is the bottom of the outer edge of the thread, then the next edge above it, and then the bottom of the inner edge, and then the top of the inner edge (I did not round the inside of the thread, as that is optional). I added a point for the centre top and bottom, and then used loops to create the triangles for the polyhedron function.
First the bottom, joining the centre to each point at the bottom of the thread.
Of course you need the internal thread too, within a nut, for example. In this case I take a clearance value to add extra spacing so that the model will work when 3D printed.
P.S. As someone has commented, there are thread libraries on thingiverse, including some very odd ones, but this was more about education - learning how exactly to use OpenSCAD and the polygon and polyhedron functions like this.