I have always found the Java synchronised statements to be a clean way of doing mutex like lock and unlocks:
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
Though the fundamental concept of monitors used in Java and pthread mutexes are different, pthread mutexes at their most basic are often used as:
void addName(char* name) {
int status = -1;
status = pthread_mutex_lock(this->lock);
if (status == 0) {
this->lastName = name;
this->nameCount++;
pthread_mutex_unlock(this->lock);
}
nameList->add(name);
}
I understand that the above code doesn't really exploit the capabilities of pthread mutexes. Nor does it handle all the error scenarios. However, this is probably the most common way of using pthread mutexes. Saying that, I think it would be nice to have a cleaner idiom for such synchronisations:
public void addName(char* name) {
synchronized(this->lock) {
this->lastName = name;
this->nameCount++;
}
nameList.add(name);
}
So is it possible to do this in C, C++?