Is it halachically permitted for a man to exercise in a gym in which there are women who are not dressed modestly? Does it make a difference if there are no other gyms available for the person to go to? What about if there are other options, but they are economically burdensome (e.g. one can go to a mixed gym for free vs. pay to go to a single gender gym or to get one's own equipment)?
If we are talking about somebody who suspects that he may fall to temptation and have inappropriate thoughts about the immodestly attired women there, it is forbidden. There are numerous issurim involved - gazing at inappropriate things is forbidden by לא תתורו אחרי לבבכם ואחרי עיניכם, and having inappropriate thoughts is forbidden under ונשמרת מכל דבר רע (see A"Z 20b, Kesuvos 46a). [See here for further discussion]
The real question arises to someone who is confident in his ability to withstand temptation to gaze at immodest sights, and to keep his thoughts on permissible things.
The Gemara (Baba Basra 57b) writes:
׳ועוצם עיניו מראות ברע׳ - א"ר חייא בר אבא זה שאין מסתכל בנשים בשעה שעומדות על הכביסה. היכי דמי: אי דאיכא דרכא אחריתא, רשע הוא אי דליכא דרכא אחריתא, אנוס הוא? לעולם דליכא דרכא אחריתא ואפ"ה מיבעי ליה למינס נפשיה
One who has two paths available, and takes the path where there are women washing is categorized as a רשע - a wicked person - although he averted his gaze and did not look at the inappropriate sights. The Rashbam explains, this is because one is forbidden to enter into a situation where there may be tempation; he is wicked for putting himself into a nisayon.
רשע הוא - ואף על פי שעוצם עיניו שלא היה לו לקרב אלא להרחיק מן העבירה דקיימא לן חולין הרחק מן הכיעור - רשב״ם בבא בתרא נ״ז
Accordingly, if one has an alternative option, one may not willingly enter a situation where he increases his temptation. If he can exercise in a place where there will not be inappropriately clad women, he must do so.
If there is no other way for him to exercise, this is considered ליכא דרכא אחרינא, and when faced without any option, the prohibition of entering a nisayon falls away - provided that he is confident he will not transgress any of the aformentioned prohibitions. Even so, R' Moshe Feinstien rules (Igros Moshe EH V1, §56) that in a recreational capacity, it is not considered ליכא דרכא אחרינא.
ולכן צריך לומר דכיון דהאיסור הוא רק משום חשש שיבוא לידי הרהור רשאי לסמוך על עצמו שיסיח דעתו מהן ולא יהרהר מחמת שיסתכל בהן כשיש לו צורך ללכת שם לצרכי פרנסתו וכדומה ואינו מחוייב לחשוש ע"ז ביותר ולהפסיד ממונו ולמנוע משאר צרכיו, ורק כשאין לו צורך לעבור כאן כגון באיכא דרכא אחריתא וה"ה כשאין לו צורך לילך שם דהולך רק לטייל שאף בליכא דרכא אחריתא הוא אסור לסמוך על עצמו שיסיח דעתו דהא זהו האיסור ונשמרת שלא יסמוך על עצמו לומר שלא יהרהר אבל במקום צורך רשאי לסמוך שלא יהרהר.
TL;DR: best to CYLOR
In an answer titled "Men and Mixed Gyms," dinonline.org writes:
There are two issues with a man going to a mixed gym. The first one is the same a going to a mixed beach- he will see women when they are dressed immodestly. The torah tells us, (Numbers 15-39) “Not to stay after our eyes”, and not to look at woman Included in this is to look at women when they are immodestly dressed.
The second issue is even if the women are totally dressed, the man will see the women exercising and in positions that will cause him to have immodest thoughts, which is also prohibited.
Sources: Y:D 21-1 Bais Shmuel ibid 2, from Rabeinu Yonah
He also addresses a woman going to a mixed gym, and while a little more lenient, seems to advise against it.
yeshiva.co in "Mixed Gym" writes:
There could be two problems with using a mixed gym:
The first is having to see inappropriately clothed women.
The second problem is the obligation to act in a fashion that lives up to people's expectations of what a good religious Jew should be doing.
R' David Sperling says problem #1 can be solved by:
... using the gym at hours where very few people are there, and you could find a spot where you could avoid [shmiras einayim issues].
In terms of #2:
Generally religious Jews use separate gyms for men and women (for the reason we wrote above), and even if you use a mixed gym in a halachically acceptable fashion, if others could think you were doing the wrong thing, it would be forbidden. This law depends both on who you are, and what the situation is where you live.
yeshiva.co provides an additional answer (R' Yoel Lieberman) and the thrust seems to be one should determine "what applies to you according to Halacha and what is best for maintaining the holiness of your neshama."