I am having timing-specific problems with a process-shared mutex in FreeBSD 12.3; sometimes pthread_mutex_lock or pthread_mutex_unlock will throw an exception. I have seen comments that process-shared mutexes were not supported in some older versions of FreeBSD, but it appears it should be supported in version 12. Is there something wrong with how I am using the pthread_mutex_t calls on FreeBSD?
See shm_open() and pthread_mutex_init()
Mutex helper class psuedocode:
Usage code
See shm_open() and pthread_mutex_init()
Mutex helper class psuedocode:
Code:
struct My_shared_data_t
{
pthread_mutex_t mutex;
pthread_cond_t cond;
// etc.
};
class My_mutex_helper_class
{
public:
My_mutex_helper_class(const string &name)
{
if (shared_memory_exists(name))
{
pointer = get_shared_memory(name); // Uses shm_open
}
else
{
pointer = create_shared_memory(name); // Uses shm_open
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
pthread_mutex_init(&pointer->mutex, &attr);
}
}
wait() // with different overloads...
{
pthread_mutex_lock(&pointer->mutex);
pthread_cond_wait(&pointer->cond, &pointer->mutex);
// ... or similar pthread_cond_timedwait
pthread_mutex_unlock(&pointer->mutex);
}
signal()
{
pthread_mutex_lock(&pointer->mutex);
pthread_cond_signal(&pointer->cond);
pthread_mutex_unlock(&pointer->mutex);
}
private:
Shared_memory_wrapper<My_shared_data_t> pointer;
}
Usage code
Code:
TEST(Event, SignalsFromNewProcess)
{
My_mutex_helper_class helper("shared_name_a");
int pid = fork();
EXPECT_NE(pid, -1);
if (pid == 0)
{
char * argv;
argv = (char *) "signal";
::execvp("./some_other_process", &argv);
// Other process:
// My_mutex_helper_class helper2("shared_name_a");
// for (int i = 0; i < 10; i++)
// {
// helper2.signal();
// Thread::sleep(10);
// }
}
else
{
// Original process
for (int i = 0; i < 20; i++)
{
helper.wait(20);
}
}
}