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 03-21-2013, 01:45 AM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
Is class static method thread safe?


Code:
class MyClass
{
public:
	static int DoSomething()
	{
		int retval = 0;
		pthread_mutex_lock(&m_mutex);
		//here is the code to do a very short cpu work
		retval = result;
		pthread_mutex_unlock(&m_mutex);
		
		return retval;
	}
private:
	static pthread_mutex_t m_mutex;
};

void* worker_thread(void* param)
{
	int retval = DoSomething();
	pthread_exit((void*)0);
}
Is the class static method DoSomething thread safe?

Last edited by fantasy1215; 03-21-2013 at 01:46 AM.
 
Old 03-21-2013, 03:16 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
it depends on //here is the code to do a very short cpu work

if
//here is the code to do a very short cpu work
shares data that is access from other locations and that is not proteced, than possible not
if it does share nothing than there is nothing to prodect

btw
if
//here is the code to do a very short cpu work
throws an exception that you do not handle than your mutex will never be unlocked

Last edited by a4z; 03-21-2013 at 03:17 AM.
 
1 members found this post helpful.
Old 03-21-2013, 03:35 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,843

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
with other words: //here is the code to do a very short cpu work: you can call/use only another thread safe class/function/method/construct
also you need to link thread safe libs, otherwise it won't work
 
Old 03-21-2013, 03:38 AM   #4
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Original Poster
Rep: Reputation: Disabled
Oh, thread safe is such a headache thing. Gurus, Can you give me some good links about thread safe topic? Thanks in advance.
 
Old 03-21-2013, 03:45 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,843

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I do not know what do you really mean by that
https://www.linuxquestions.org/quest...d-safe-317273/
https://en.wikipedia.org/wiki/Thread_safety

Threads are much "cheaper" than processes and also threads can much more easily communicate with each other (than processes).
 
Old 03-21-2013, 08:29 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Let's assume that you are talking about threads, not processes.

The mere fact that something is a "static method" really doesn't mean anything at all. Pragmatically, contention and conflict among multiple threads must be prevented. Typically, this is done in the fundamental design of the processes themselves, not by the classes etc. which they use. You have to know where threads could possibly "race" with one another, and take appropriate steps in your design to prevent those races.

I'm waving my hands at this point because the topic just got very large. However, the immediate answer to your immediate question is: N-o.
 
Old 03-21-2013, 10:09 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
The best way to make anything thread-safe is to avoid them like the plague.
IMHO
 
  


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
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
Java: what's the difference between an inner class and a static inner class? irey Programming 6 01-28-2009 03:34 AM
How to initialize a static array of a class in a static member function lali.p Programming 9 02-16-2008 09:27 AM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
PHP: how to use a method from a class in the same class ldp Programming 5 09-17-2004 09:52 AM

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

All times are GMT -5. The time now is 12:17 AM.

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