The following contains spoilers for “Avengers: Infinity War.”

Thanos, the uber-villain of the Marvel Cinematic Universe, finally arrived in “Avengers: Infinity War.” He made his intentions clear: The universe, he says, is overpopulated and its consumption of natural resources unsustainable. His solution? Kill 50 percent of the population in an entirely random lottery. A mass, but impartial, genocide.

The world’s mightiest heroes have faced villains with grandiose plans before. But the scale of destruction Thanos enacts truly seems the stuff of comic books. Thanos snaps his fingers and half of our beloved MCU characters dissolve into ash. In the post-credits scene, we begin to see the larger consequences: cars careen off the road and helicopters fall from the sky.

As the lights come up, we are left to imagine the impact not just on superheroes but on everyday people. How would we react if half of our friends, family and the rest of the world suddenly died? What horrors would unfold? And would it — could it — actually create a sustainable world?

History books, not comics, have the answer. Seven centuries ago, the Black Death killed a massive swath of the world’s people, including an estimated 50 percent of medieval Europe’s population in little more than four years. In that time, it upended the social order, ground the economy to a halt and caused unfathomable suffering. As famed Arab historian Ibn Khaldun wrote in the years immediately following the plague, “It swallowed up many of the good things of civilization and wiped them out in the entire habited world.”

The plague first ravaged the great civilizations of Mongolia, China, India and the Middle East. By 1347, it arrived in Europe via Genoese trading ships, and from there, it spread like wildfire. In 1348, Agnolo di Tura del Grasso wrote a personal account of the plague in Italy. Describing it as “a cruel and horrible thing,” he detailed the ways it severed traditional social bonds. “It seemed to almost everyone that one became stupefied by seeing the pain,” he reported. “Father abandoned child, wife husband, one brother another; for this illness seemed to strike through the breath and sight. And so they died. And none could be found to bury the dead for money or friendship.”

The death toll did more than just shatter families — it destroyed hope. “So many died,” del Grasso wrote, “that all believed that it was the end of the world.”

Like Thanos, the pestilence was indiscriminate. Though some attempted to escape to the countryside (as Italian writer Giovanni Boccaccio recounts in “The Decameron”), the pestilence followed. It struck down rich and poor, old and young, pious and impious.

The half who survived were irrevocably changed. Some took extreme measures to offer up their physical bodies as an act of penance, an attempt to persuade God to put an end to seemingly endless deaths. French author Jean Froissart documented groups emerging from Germany that “scourged themselves with whips of hard knotted leather with little iron spikes” in a radical attempt to beg forgiveness from God and bring an end to the plague.

And as happens all too often during times of crisis, minority communities came under assault. Many Christians, looking for someone to blame, turned on Jewish communities in their midst. This led to a spike in anti-Jewish propaganda and violence that ultimately led to the murder of thousands of Jewish men, women and children.

Others took a different tack. Living in the immediate aftermath of the pestilence, Italian writer Matteo Villani captured the indulgent nihilism of some survivors. “Since men were few,” he wrote, “and since, by hereditary succession, they abounded in earthly goods, they forgot the past as though it had never been, and gave themselves up to a more shameful and disordered life than they had led before.” Villani detailed their gluttony: clogging taverns with orgies of food and drink, risking their health and safety in dangerous games, indulging in sex and “unbridled lechery.”

Nor was it only the wealthy who found themselves newly freed. “Common folk,” Villani noted, “by reason of the abundance and superfluity that they found, would no longer labor at their accustomed trades, but demanded the dearest and most delicate foods for their sustenance; and they married at their will, while children and common women clad themselves in all the fair and costly garments of the ladies dead by that horrible death.” The norms of morality and class hierarchies had suddenly fallen away to be replaced with hedonism.

Pre-plague medieval society was, like ours today, organized around systems that embraced vast income inequalities, exploited cheap labor and created a scarcity of luxury goods. With the population halved, the traumatized survivors were made temporarily wealthy as they inherited the wreckage of the past. But it wasn’t to last: With so few craftspeople left and the labor force massively diminished, the economy unraveled.

In the film, Thanos fantasizes that his genocide would result in a more ecologically sustainable world and that he would ultimately “watch the sun rise on a grateful universe.” And by the end, Thanos gets his wish — though whether the universe is grateful, and whether his goal of creating a more sustainable universe has actually been met, are other questions entirely.

Medieval historians might be more ambivalent than Thanos about the outcome of his genocide. Yes, the Black Death ushered in some positive social changes, but the cost was incredibly high. In his book “The Black Death,” historian Robert S. Gottfried describes the disruptive effects of the plague: “The old constitutional, governmental, and commercial institutions, old philosophical notions, and the systems of religious belief came under massive — and, frequently, successful — challenge.”

The old order was indeed undone. That was not necessarily a bad thing, in Gottfried’s telling. Old class boundaries crumbled as “cheap, abundant human labor” disappeared. New technologies and new equalities arose in its place. The shortage of cheap labor helped break the system of serfdom, and promoted the growth of the middle class.

But unlike what Thanos seems to expect of the universe, the new world that rose from the ashes of the Black Death was not a more ecologically sustainable one. It did not result in reduced consumption of natural resources long term, and notably, within a handful of generations, the population of Europe rebounded completely.

There is also a significant moral difference between an unthinking plague and a willing actor. As Agnolo di Tura del Grasso would tell you, mass death, even arbitrary or for a “greater good,” is not worth having to bury your children with your own hands, as he did.

So while the Avengers are likely to undo all of Thanos’s destruction in their next film, medieval history shows us that even if our heroes fail, life will eventually return to normal. This is a glimmer of hope, even in the face of the real-world possibility of pandemics induced by antibiotic-resistant diseases or bioterrorism.

Thanos would do well to read some history before snapping his fingers.