Eighty thousand black people living around this picturesque city have no homes, have no place else to go and are barred by law from constructing shelters in which to live.

For two days this week an area of sandy dunes outside the city teetered on the edge of a race riot in the latest round of violence triggered by police trying to tear down the huts that people build.

The problem has its roots in a concerted but futile effort, begun 20 years ago, to turn the western half of Cape Province into a zone free of black people. Because of this goal--one facet of South Africa's segregationist policy, called apartheid--no more houses were built for blacks in the area around Cape Town known as the Cape Peninsula, which is the region's most industrialized area.

But the policy failed spectacularly. The population of blacks has quadrupled in the 20 years, partly because of the demand for labor, producing an unmanageable number of homeless.

Government officials, accompanied by policemen with tear gas and dogs, Tuesday and Wednesday tore down crude wood-and-plastic shelters in which about 600 African families were living on the dunes.

In their anger, the homeless people threw stones at the officials and smashed a white reporter's car.

Ten miles away in South Africa's all-white Parliament, opposition speakers accused the government yesterday of pursuing an inhumane policy toward the dune dwellers. After an angry debate, the minister in charge of black affairs, Pieter G. Koornhof, promised to provide rudimentary services for those whose shacks had just been demolished.

This has become a recurring scenario in the Cape Peninsula: government officials knock down the squatters' homes, resulting in a political outcry, which usually leads to a piecemeal concession. The cycle has been going on for more than six years.

Despite the policy's manifest failure, the government is still reluctant to accept the permanence of the blacks who are here. Restricting the entry of blacks to all the towns and cities of South Africa is the most fundamental feature of apartheid. The idea is to limit the numbers to the minimum needed by the urban economy.

The rest are supposed to stay in the tribal reserves, which are now called "homelands" and are earmarked for national independence.

Only blacks who can meet one of the following qualifications are given the legal right to live permanently in an urban area: those who were born in the area; those who have worked for one employer in the area continuously for 10 years; and those who have worked for more than one employer in the area for 15 years.

Others can come into the cities only as migrant workers, on one-year contracts at the end of which they must return to the homeland and register again as work seekers. They may not take their families to the city, and they live there in large single-sex hostels.

That is the general system for the country as a whole, but for the western half of Cape Province much stricter additional restrictions apply.

In 1962 a line, named the Eiselen Line after the man who was then secretary of the African Affairs Department, was drawn from the town of Colesburg to Humansdorp, near Port Elizabeth.

The area west of that line, about half the province, was proclaimed a "Colored preference area," meaning the mixed-race Colored people were to be given preference over blacks in getting jobs there.

By law an employer in the area may hire a black only if he can satisfy the Department of Manpower that there is no Colored person available who can do the job. In 1966 employers were also instructed to reduce their quotas of black labor in the area by 5 percent a year. Government policy was "to reduce, halt and ultimately reverse the flow of blacks to the western cape."

The reason, as expressed by Secretary W.W.M. Eiselen at the time, was that "the Coloreds, as the local population, have the moral right to demand that their field of employment be protected from the blacks. Also early termination of the symbiosis of black and Colored is advisable, because it has a demoralizing effect upon both races."

Some people suspect another, unstated, motive was to try to prepare the western cape as a possible future emergency escape hatch for the whites.

In line with this policy, the building of houses for blacks in the area was stopped at the same time.

Property rights for blacks are severely limited everywhere in the country, but in the western cape blacks may not own property at all. They can only live in houses rented from the government. Between 1962 and 1976, only 330 new houses for rent to blacks were built in the Cape Peninsula, which now has a black population of 274,000.

But all this ideological theory ran against economic reality in what is a rapidly industrializing country. Despite all the laws and controls, the labor demands of the expanding economy continued to suck in thousands of blacks to all the cities, including the western cape area.

At the height of the restrictive effort, from 1968 to 1974, the em-ployment of blacks increased by 200 percent and in the government service by 323 percent.

With no houses for the swelling number of workers to live in, the squatter camps with their rudimentary shacks began to mushroom all over the dunes outside Cape Town, and government officials began their Sisyphean task of knocking them down and watching them reappear.

Periodically a concession to the inevitable is made. In 1979 Koornhof relented in the face of an international outcry against the demolition of a squatter camp called Crossroads, which then housed about 10,000 people. He allowed Crossroads to remain but said he would not permit any other squatter camps to become permanent.

There was another outcry in August 1981 when the officials demolished squatter homes in the midst of a bitter cape winter and began deporting 2,017 people to the nominally independent Transkei tribal homeland, 800 miles away.

A delegation of U.S. congressmen who were visiting South Africa at the time and witnessed part of the removal issued a public statement condemning the "official inhumanity and brutality" of the authorities.

Transkei's President Kaizer Matanzima refused to accept the squatters when they arrived there, saying he had neither housing nor jobs for them. He put them on buses back to Cape Town, but the South African police set up roadblocks to prevent their return.

Eventually most managed to make their way back, and today they live on the dunes in a tent camp provided by a church organization.

Last March, 55 squatters, who repeatedly had been rendered homeless and arrested, entered Cape Town's St. George's Anglican Cathedral and began a protest fast.

After 24 days, by which time some were becoming dangerously weak, Koornhof agreed to examine their cases. Today they are allowed to live in another tent town on the dunes.

Two years ago the house-building freeze was eased, and 2,900 new houses have been built since then with another 9,200 planned by 1991.

The policy of demolishing the shacks remains, however.

Timo Bezuidenhout, chief commissioner of the euphemistically named Department of Cooperation and Development and the man in charge of the operation, insists that the policy is not inhumane.

He said in an interview that he is convinced that it is in the best long-term interests of the squatters themselves. Bezuidenhout explained that the land where the squatter shacks appeared last week is earmarked for new housing.

"If I let them stay I will never be able to build the new houses there," he said.

Nor can they be allowed to build new shacks anywhere else, because all the land in the area is earmarked for new housing, Bezuidenhout said.

"I am just appealing to them to be patient, to give me a chance, and to go back to lodge with other families where they were before," he said.

"Believe me, all I want is to see that the people who are entitled to be here can live in decent circumstances."

Squatters interviewed at the site of the demolitions Wednesday said they would not go back to lodge with other families because the houses had become unbearably overcrowded.

Matwa Mnyamana, a 43-year-old father of five who has a job in a Cape Town supermarket, said his family had been living with another in a two-roomed house. There were 14 people in the house.

"I got out and built a shack," he said. "I have had my shacks knocked down seven times, but I would rather go on building them than go back into that house."

A number of squatters interviewed said they believed the government's policy of demolition was intended to harass them into leaving the area and returning to a homeland.

Bezuidenhout denied this. "Don't want to get sentimental," he said, "but I have worked with and for these people for 35 years, and I would like to see them having a decent living.

"I am talking about those who are entitled to be here. We have got to curb the influx. This problem of influx is world-wide. It appears all over Africa. Nigeria has just put out 2 million people. I am not trying to hide behind those facts. I am just trying to point out that it is not so damned easy to curb the situation."