ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.