mercredi 25 mars 2015

Is it safe to declare a mutex at file-scope?

I realize this question is similar, but mine is not specific to Windows.


Is is safe and good form to declare a mutex at file-scope like so?



#include <iostream>
#include <mutex>
#include "Logger.h"

mutex my_mutex;

void Logger::log()
{
lock_guard<mutex> lock(my_mutex);
for (int i = 0; i < 29; ++i)
{
cout << i << " ";
}
cout << endl;
}


I want to synchronize at instance-level, not across instances. I tested it and it worked fine. Are there any gotchas?


Aucun commentaire:

Enregistrer un commentaire