 
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://adonaimedrado.pro.br/wiki/index.php?action=history&amp;feed=atom&amp;title=C%3A_Starvation</id>
		<title>C: Starvation - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://adonaimedrado.pro.br/wiki/index.php?action=history&amp;feed=atom&amp;title=C%3A_Starvation"/>
		<link rel="alternate" type="text/html" href="https://adonaimedrado.pro.br/wiki/index.php?title=C:_Starvation&amp;action=history"/>
		<updated>2026-05-27T18:03:56Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>https://adonaimedrado.pro.br/wiki/index.php?title=C:_Starvation&amp;diff=3650&amp;oldid=prev</id>
		<title>Adonaimedrado: Created page with '&lt;code lang=c&gt; #include &lt;stdio.h&gt; #include &lt;pthread.h&gt;  #define BOOL int #define TRUE 1 #define FALSE 0  int x; pthread_mutex_t mutex_de_x, mutex_de_y;  void *minha_thread1(void *…'</title>
		<link rel="alternate" type="text/html" href="https://adonaimedrado.pro.br/wiki/index.php?title=C:_Starvation&amp;diff=3650&amp;oldid=prev"/>
				<updated>2010-08-28T07:14:46Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;&amp;lt;code lang=c&amp;gt; #include &amp;lt;stdio.h&amp;gt; #include &amp;lt;pthread.h&amp;gt;  #define BOOL int #define TRUE 1 #define FALSE 0  int x; pthread_mutex_t mutex_de_x, mutex_de_y;  void *minha_thread1(void *…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;code lang=c&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BOOL int&lt;br /&gt;
#define TRUE 1&lt;br /&gt;
#define FALSE 0&lt;br /&gt;
&lt;br /&gt;
int x;&lt;br /&gt;
pthread_mutex_t mutex_de_x, mutex_de_y;&lt;br /&gt;
&lt;br /&gt;
void *minha_thread1(void *null)&lt;br /&gt;
{	&lt;br /&gt;
	BOOL consegui_lock_em_x=FALSE, consegui_lock_em_y=FALSE;&lt;br /&gt;
&lt;br /&gt;
	while(!consegui_lock_em_x || !consegui_lock_em_y)&lt;br /&gt;
	{&lt;br /&gt;
		consegui_lock_em_x=FALSE;&lt;br /&gt;
		consegui_lock_em_y=FALSE;&lt;br /&gt;
&lt;br /&gt;
		if (!pthread_mutex_trylock(&amp;amp;mutex_de_x))&lt;br /&gt;
		{&lt;br /&gt;
			consegui_lock_em_x=TRUE;&lt;br /&gt;
			printf(&amp;quot;minha_thread1: Obtive acesso ao mutex X.\n&amp;quot;);&lt;br /&gt;
			printf(&amp;quot;minha_thread1: Aguardando 1 segundo...\n&amp;quot;);&lt;br /&gt;
			sleep(1);&lt;br /&gt;
			printf(&amp;quot;minha_thread1: Tentando mutex Y...\n&amp;quot;);		&lt;br /&gt;
			if (!pthread_mutex_trylock(&amp;amp;mutex_de_y))&lt;br /&gt;
			{&lt;br /&gt;
				consegui_lock_em_y=TRUE;&lt;br /&gt;
				printf(&amp;quot;minha_thread1: Obtive acesso ao mutex Y.\n&amp;quot;);&lt;br /&gt;
				printf(&amp;quot;minha_thread1: Liberando mutex Y...\n&amp;quot;);&lt;br /&gt;
				pthread_mutex_unlock(&amp;amp;mutex_de_y);&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
			{&lt;br /&gt;
				printf(&amp;quot;minha_thread1: Não obtive acesso ao mutex Y.\n&amp;quot;);&lt;br /&gt;
				consegui_lock_em_y=FALSE;&lt;br /&gt;
			}&lt;br /&gt;
			printf(&amp;quot;minha_thread1: Aguardando 1 segundo...\n&amp;quot;);&lt;br /&gt;
			sleep(1);&lt;br /&gt;
			printf(&amp;quot;minha_thread1: Liberando mutex X...\n&amp;quot;);&lt;br /&gt;
			pthread_mutex_unlock(&amp;amp;mutex_de_x);&lt;br /&gt;
		}&lt;br /&gt;
		else&lt;br /&gt;
			consegui_lock_em_x=FALSE;&lt;br /&gt;
&lt;br /&gt;
		printf(&amp;quot;minha_thread1: consegui_lock_em_x=%d,consegui_lock_em_y=%d\n&amp;quot;,consegui_lock_em_x,consegui_lock_em_y);&lt;br /&gt;
		sleep(1);		&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	pthread_exit(NULL);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void *minha_thread2(void *null)&lt;br /&gt;
{	&lt;br /&gt;
	BOOL consegui_lock_em_x=FALSE, consegui_lock_em_y=FALSE;&lt;br /&gt;
&lt;br /&gt;
	while(!consegui_lock_em_x || !consegui_lock_em_y)&lt;br /&gt;
	{&lt;br /&gt;
		consegui_lock_em_x=FALSE;&lt;br /&gt;
		consegui_lock_em_y=FALSE;&lt;br /&gt;
&lt;br /&gt;
		if (!pthread_mutex_trylock(&amp;amp;mutex_de_y))&lt;br /&gt;
		{&lt;br /&gt;
			consegui_lock_em_y=TRUE;&lt;br /&gt;
			printf(&amp;quot;\tminha_thread2: Obtive acesso ao mutex Y.\n&amp;quot;);&lt;br /&gt;
			printf(&amp;quot;\tminha_thread2: Aguardando 1 segundo...\n&amp;quot;);&lt;br /&gt;
			sleep(1);&lt;br /&gt;
			printf(&amp;quot;\tminha_thread2: Tentando mutex X...\n&amp;quot;);		&lt;br /&gt;
			if (!pthread_mutex_trylock(&amp;amp;mutex_de_x))&lt;br /&gt;
			{&lt;br /&gt;
				consegui_lock_em_y=TRUE;&lt;br /&gt;
				printf(&amp;quot;\tminha_thread2: Obtive acesso ao mutex X.\n&amp;quot;);				&lt;br /&gt;
				printf(&amp;quot;\tminha_thread2: Liberando mutex X...\n&amp;quot;);&lt;br /&gt;
				pthread_mutex_unlock(&amp;amp;mutex_de_x);&lt;br /&gt;
			}&lt;br /&gt;
			{&lt;br /&gt;
				printf(&amp;quot;\tminha_thread2: Não obtive acesso ao mutex X.\n&amp;quot;);&lt;br /&gt;
				consegui_lock_em_x=FALSE;&lt;br /&gt;
			}&lt;br /&gt;
			printf(&amp;quot;\tminha_thread2: Aguardando 1 segundo...\n&amp;quot;);&lt;br /&gt;
			sleep(1);&lt;br /&gt;
			printf(&amp;quot;\tminha_thread2: Liberando mutex Y...\n&amp;quot;);&lt;br /&gt;
			pthread_mutex_unlock(&amp;amp;mutex_de_y);&lt;br /&gt;
		}&lt;br /&gt;
		else&lt;br /&gt;
			consegui_lock_em_y=FALSE;&lt;br /&gt;
&lt;br /&gt;
		printf(&amp;quot;\tminha_thread2: consegui_lock_em_x=%d,consegui_lock_em_y=%d\n&amp;quot;,consegui_lock_em_x,consegui_lock_em_y);&lt;br /&gt;
		sleep(1);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	pthread_exit(NULL);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
	int retorno_da_thread;&lt;br /&gt;
	pthread_t thread1, thread2;&lt;br /&gt;
	pthread_attr_t atributo_de_thread;&lt;br /&gt;
&lt;br /&gt;
	pthread_attr_init(&amp;amp;atributo_de_thread);&lt;br /&gt;
	pthread_attr_setdetachstate(&lt;br /&gt;
		&amp;amp;atributo_de_thread, PTHREAD_CREATE_JOINABLE);&lt;br /&gt;
&lt;br /&gt;
	pthread_mutex_init(&amp;amp;mutex_de_x,NULL);&lt;br /&gt;
	pthread_mutex_init(&amp;amp;mutex_de_y,NULL);&lt;br /&gt;
	&lt;br /&gt;
	if (pthread_create(&amp;amp;thread1,&lt;br /&gt;
		&amp;amp;atributo_de_thread,&lt;br /&gt;
		minha_thread1,NULL))&lt;br /&gt;
		printf(&amp;quot;Erro ao criar a minha_thread1.\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	if (pthread_create(&amp;amp;thread2,&lt;br /&gt;
		&amp;amp;atributo_de_thread,minha_thread2,NULL))&lt;br /&gt;
		printf(&amp;quot;Erro ao criar a minha_thread2.\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	if (pthread_join(thread1,(void **)&amp;amp;retorno_da_thread))&lt;br /&gt;
		printf(&amp;quot;Erro no join de minha_thread1.\n&amp;quot;);&lt;br /&gt;
	else&lt;br /&gt;
		printf(&amp;quot;minha_thread1 joined.\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	if (pthread_join(thread2,(void **)&amp;amp;retorno_da_thread))&lt;br /&gt;
		printf(&amp;quot;Erro no join de minha_thread2.\n&amp;quot;);&lt;br /&gt;
	else&lt;br /&gt;
		printf(&amp;quot;minha_thread2 joined.\n&amp;quot;);&lt;br /&gt;
		&lt;br /&gt;
	pthread_mutex_destroy(&amp;amp;mutex_de_y);&lt;br /&gt;
	pthread_mutex_destroy(&amp;amp;mutex_de_x);&lt;br /&gt;
&lt;br /&gt;
	pthread_attr_destroy(&amp;amp;atributo_de_thread);&lt;br /&gt;
&lt;br /&gt;
	pthread_exit(NULL);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Adonaimedrado</name></author>	</entry>

	</feed>