Saturday, August 1, 2015

Digest for comp.lang.c++@googlegroups.com - 5 updates in 2 topics

Doug Mika <dougmmika@gmail.com>: Aug 01 11:43AM -0700

Quick question:
 
explicit variable decleration (I explicitly declare variable dummyLck):
unique_lock<std::mutex> dummyLck(dummyMtx); //of course this works
 
but I want to declare the dummyLck as an "implicit" variable without a name, and pass it directly to a condition variable, but the following line spits syntax errors...what's the syntax for doing this:
condition_variable threadsReady;
mutex dummyMtx;
threadsReady.wait(unique_lock<std::mutex>(dummyMtx)); //this syntax seems to be wrong, but why?
Bo Persson <bop@gmb.dk>: Aug 01 09:22PM +0200

On 2015-08-01 20:43, Doug Mika wrote:
> condition_variable threadsReady;
> mutex dummyMtx;
> threadsReady.wait(unique_lock<std::mutex>(dummyMtx)); //this syntax seems to be wrong, but why?
 
It's not the syntax, it's the semantics. The wait function takes a
non-const reference to a lock. That will not bind to a temporary.
 
 
 
Bo Persson
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Aug 02 12:23AM +0200

On 01-Aug-15 9:22 PM, Bo Persson wrote:
> On 2015-08-01 20:43, Doug Mika wrote:
[snipped]
>> seems to be wrong, but why?
 
> It's not the syntax, it's the semantics. The wait function takes a
> non-const reference to a lock. That will not bind to a temporary.
 
One way to obtain a reference to a temporary is
 
template< class Type >
auto temp_reference( Type&& o )
-> Type&
{ return o; }
 
The lifetime of the temporary extends to the end of the full-expression.
 
I vaguely remember that Dietmar Kuhl (I think it was, anyway I place the
blame for every ingenious insight on him) once showed a standard library
function that could do this. But I can't find it now. It's not `std::ref`.
 
I even more vaguely remember that there were some special related C++03
tricks for `std::string` and `std::ostringstream`, but for that, the
details elude me completely -- possibly something to do with `append`
for strings?
 
 
Cheers,
 
- Alf
 
 
--
Using Thunderbird as Usenet client, Eternal September as NNTP server.
Luca Risolia <luca.risolia@linux-projects.org>: Aug 01 02:16PM +0200

Il 31/07/2015 20:37, Doug Mika ha scritto:
> I guess what I wanted to ask is is there a structure/way in C++ to
"bundle" commands into "one" command ensuring that once I start
executing the first command that no thread will interrupt until I
execute the last command in the bundle?
> stack.pop();
> }
> /*end of bundle*/
 
Use a monitor:
 
template<class T>
class monitor {
private:
mutable T t;
mutable std::mutex m;
public:
monitor(T t_ = T{}) : t{t_} {}
template<class F>
decltype(auto) operator()(F f) const {
std::lock_guard<std::mutex> _{m};
return f(t);
}
};
 
// For example:
monitor<std::stack<int>> sync_stack;
sync_stack([](auto& s) {
if (!s.empty()){
/*some code*/
s.pop();
}
});
Richard Damon <Richard@Damon-Family.org>: Aug 01 12:18PM -0400

On 7/31/15 2:37 PM, Doug Mika wrote:
> /*end of bundle*/
 
> out of the collection of commands I wanted to create "one command"
> that is guaranteed to be executed without interruptions by ANY other thread?
 
Since the model used in C++ includes support for multi-core processors
(supporting memory barriers in addition to access ordering), the other
thread could access the stack without "interrupting" the current thread.
 
Thus, the only way to achieve what you are asking for would not only
need to prevent a "context swap", but also suspend ALL other processor
cores.
 
The real solution is to write the program properly, and properly wrap
the accesses to stack with an appropriate synchronizing operations for
ALL accesses.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: