A theory of thermally activated flux creep in nonideal type II superconductors