Page MenuHome

Use guard_lock, rather than unique_lock when the functionality of the latter is not needed
Needs RevisionPublic

Authored by John Pavel (jrp) on Apr 17 2014, 10:42 PM.

Details

Summary

I have adapted util_threads.h to use C++11 threads and locks as a drop-in substitute for the boost versions, avoiding the use of pthread with VC++2013 (see below). This patch replaces the use of unique_lock with the lighter lock_guard. This probably makes little difference to performance (as they both uses the same underlying mutexes) but it makes the intention of the code a bit clearer where no explicit (un)locking or functionality such as waiting is required.

#ifdef CCL_USE_CPP11
#include <thread>
#include <condition_variable>
#include <mutex>
#include <queue>

#include "util_function.h"

CCL_NAMESPACE_BEGIN

/* use boost for mutexes, thread */

typedef std::mutex thread_mutex;
typedef std::unique_lock<thread_mutex> thread_scoped_lock;
typedef std::lock_guard<thread_mutex> thread_guard_lock; /* lighter weight than unique lock, documents intent */
typedef std::condition_variable thread_condition_variable;
typedef std::thread thread;

#else

#include <boost/thread.hpp>
#include <pthread.h>
#include <queue>

#include "util_function.h"

CCL_NAMESPACE_BEGIN

/* use boost for mutexes */

typedef boost::mutex thread_mutex;
typedef boost::mutex::scoped_lock thread_scoped_lock;
typedef boost::mutex::scoped_lock thread_guard_lock;
typedef boost::condition_variable thread_condition_variable;
#define function_bind boost::bind

Diff Detail

Event Timeline

And a few more lock_guards missed first time around

Looks good to me, will commit in a moment.

Brecht Van Lommel (brecht) requested changes to this revision.Apr 18 2014, 2:58 PM

Actually this patch seems to be incomplete, can you give a patch that applies on the current master?

You can replace the current one by creating a new diff, and then in the second step attaching it to this revision D466:
https://developer.blender.org/differential/diff/create/

John Pavel (jrp) updated this revision to Unknown Object (????).Apr 18 2014, 9:28 PM

Apologies; I though that these were already in the origin.

Thomas Dinges (dingto) requested changes to this revision.Apr 18 2014, 11:24 PM

I think someone went wrong here, suddenly the patch is much bigger and also consists some code that should not be there. (SSE42, or these "placeholder" variables).

I meant "something" went wrong ^^ :)

I had incorrectly assumed that the C++11 drop-in substitute for boost was already in. This replaces all the boost threading, locking, etc, with C++11 versions. They can be switched on / off with the CCL_USE_CPP11 flag.

The placeholders could go by including a using std::placeholders in the C++11 version, but I used a macro to make things explicit.

The SSE stuff is there by accident. It doesn't do anything at present, except tidy up the existing sequence of definitions.