LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-12-2011, 11:27 PM   #1
vendtagain
Member
 
Registered: Sep 2009
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199

Rep: Reputation: 32
Is it necessary to mutex lock here?(C++)


I am trying to better understand threads and mutexes.
I have two threads. If both threads only Read from a variable (and do not change it), is a mutex needed?

In that situation, is a mutex needed to prevent undefined behavior?

A simple example:
Code:
struct point
{
float x,y,z;
};

point ppp;

void firstfunc()
{
       --ppp.x;
       --ppp.y;
       --ppp.z;
}
void secondfunc()
{
       ++ppp.x;
       ++ppp.y;
       ++ppp.z;
}

main()
{
thread t1(firstfunc);
thread t2(secondfunc):
t1.join();
t2.join();
}
 
Old 08-13-2011, 12:04 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by vendtagain View Post
If both threads only Read from a variable (and do not change it), is a mutex needed?
This doesn't appear to correspond to the code in your post. Both threads modify ppp. You shouldn't need a mutex in situations where nothing in the program changes the data being read, but in the example you posted you do need a mutex.
Kevin Barry
 
1 members found this post helpful.
Old 08-14-2011, 12:20 AM   #3
vendtagain
Member
 
Registered: Sep 2009
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199

Original Poster
Rep: Reputation: 32
Oops, I changed the question a few times before posting and forgot to update the code, I did mean read only in both threads. Thanks.
 
  


Reply



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
E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable) phreakshew Linux - Newbie 24 04-12-2019 10:42 AM
Lock after invalid login attempts, session lock, minimum password length nstarz Linux - Security 1 06-24-2010 05:36 PM
Mutex lock __status g_tarun Programming 1 06-07-2007 12:00 AM
timeout for a mutex lock dmail Programming 6 10-24-2005 07:22 AM
How does pthread_mutex_lock() lock mutex in pthread icoming Programming 0 12-04-2004 08:54 AM

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

All times are GMT -5. The time now is 09:17 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