LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-01-2015, 10:26 AM   #1
varoluscuprens
LQ Newbie
 
Registered: Jul 2011
Location: Turkey
Distribution: ubuntu 14.04
Posts: 17

Rep: Reputation: Disabled
A Question About Thread Behaviour


Hi There;
I am facing a thread question.The question is what will be the output of a0, a1 and a2? It is clear that a0=500 and a1=100.The result is as expected. However, value of a2 is changing whenever I run the code. Why? Here is the code:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#define NTH 5

pthread_mutex_t mutex;
int a0,a1,a2;

void * foo(void * arg)
{

	int tindex,i;
	int x1 = 0;
	int x2 = 7;

	tindex = (int) arg;

	for(i = 0 ; i < 100 ; i++)
	{
		pthread_mutex_lock(&mutex);

		a0++;
		x1++;


		pthread_mutex_unlock(&mutex);
		x2 += tindex;
	}

	pthread_mutex_lock(&mutex);

	a1 = x1;
	a2 = x2;

	pthread_mutex_unlock(&mutex);
	pthread_exit(0);

	return NULL;
}


int main(void)
{

	int i;
	pthread_t tid[NTH];

	a0 = a1 = a2 = 0;

	pthread_mutex_init(&mutex,NULL);

	for(i=0 ; i<NTH ; ++i)
	{
		pthread_create(&tid[i] , NULL , foo , (void *) i);
	}

	for(i=0 ; i<NTH ; ++i)
	{
		pthread_join(tid[i] , NULL );
	}

	printf("a0=%d a1=%d a2=%d \n",a0,a1,a2);

	return 0;


}
Thanks in advance.
 
Old 01-01-2015, 11:18 AM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I suspect it is because x2 is incremented outside the mutex leading to race condition.
 
1 members found this post helpful.
Old 01-01-2015, 05:08 PM   #3
ljb643
Member
 
Registered: Nov 2003
Posts: 526

Rep: Reputation: Disabled
The value of a2 at the end is (7 + 100 * T), where T is the index (0 to NTH-1) of the thread which happened to exit last. Since you can't predict which thread will exit last, the value could be 7, 107, 207, 307, or 407 (with NTH=5).
 
1 members found this post helpful.
  


Reply

Tags
mutex, thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird thread behaviour brownflamigo1 Programming 4 07-25-2008 05:53 AM
Strange behaviour from kernel thread lbdgwgt Programming 2 11-08-2007 01:32 AM
Question on select behaviour exman Programming 1 08-30-2006 12:13 PM
Spamassassin Behaviour Question jonwatson Linux - Newbie 3 06-14-2006 01:48 PM
behaviour of echo with ? (the question mark) in gnome-terminal fsbooks Programming 1 01-08-2004 12:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration