Monday, August 24, 2015

Digest for comp.lang.c++@googlegroups.com - 6 updates in 3 topics

jacobnavia <jacob@jacob.remcomp.fr>: Aug 25 01:14AM +0200

Le 24/08/2015 19:31, Bo Persson a écrit :
 
> You can't trust a benchmark you haven't rigged yourself. :-)
 
> Bo Persson
 
Using the CCL (C containers library) we have: (from memory)
 
#include <containers.h>
 
int main(void)
{
strCollection *myData =
strCollection.CreateFromFile("mydata.txt");
strCollection.Sort(myData);
strCollection.WriteToFile(myData,"mySortedData.txt");
return 0;
}
 
The C containers library is written in C and features nice stuff. Fully
open source (no license) available at:
 
https://code.google.com/p/ccl/
 
Have fun!
Christopher Pisz <nospam@notanaddress.com>: Aug 24 04:58PM -0500

I am trying to write some concept code for starting a collection of
threads from the main thread that will each be somewhat encapsulated by
a class.
 
Basically, I want to move that into a service I wrote where I've got
multiple threads doing the same thing in their own loop instead of the
single thread doing one customer in a loop.
 
Problem I am having is that in order to make the main thread start
multiples, I cannot do the wait for join right after I start, but need
to iterate through a bunch of objects, start them, get the thread they
are running on, add it to a thread_group, and then wait on all of them
from the main thread.
 
The compiler is complaining "error C2280: 'boost::thread::thread(const
boost::thread &)' : attempting to reference a deleted function"
 
I am not sure how to accomplish my goal. Please advise!
 
Here is my concept code listing:
 
______________________
SomeClass.h
 
#pragma once
 
// Boost Includes
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
 
// Standard Includes
#include <atomic>
 
//--------------------------------------------------------------------------------------------------
class SomeClass
{
public:
 
typedef boost::shared_ptr<SomeClass> Shared_Ptr;
 
static boost::thread Start(SomeClass & someClass);
 
SomeClass();
~SomeClass();
 
void Stop();
 
protected:
 
void ThreadProcedure();
 
static size_t m_count;
size_t m_id;
boost::thread m_thread; // Worker thread this
object will run on
boost::function<void()> m_threadProcedure; // Function object that
is the main loop for the thread
std::atomic<bool> m_stopping; // Flag to exit loop in
thread
};
 
//--------------------------------------------------------------------------------------------------
 
__________________
SomeClass.cpp
 
#include "SomeClass.h"
 
// Boost Includes
#include <boost/thread.hpp>
 
// Standard Includes
#include <iostream>
 
//--------------------------------------------------------------------------------------------------
size_t SomeClass::m_count = 0;
 
//--------------------------------------------------------------------------------------------------
boost::thread SomeClass::Start(SomeClass & someClass)
{
// Start the thread and wait for it to exit
someClass.m_thread.swap(boost::thread(someClass.m_threadProcedure));
 
// Return the thread we started, so that the caller may wait on it
to join along with any other
// threads the started from other instances of this class.
return someClass.m_thread;
}
 
//--------------------------------------------------------------------------------------------------
void SomeClass::Stop()
{
// Flags the loop in the thread procedure to exit
m_stopping = false;
}
 
//--------------------------------------------------------------------------------------------------
SomeClass::SomeClass()
:
m_id (m_count++),
m_thread (),
// Derived classes should override this behavior by binding their
own thread procedure in their constructor
m_threadProcedure(boost::bind(&SomeClass::ThreadProcedure, this)),
m_stopping (false)
{
std::cout << "SomeClass #" << m_id << " constructor has been
called." << std::endl;
}
 
//--------------------------------------------------------------------------------------------------
SomeClass::~SomeClass()
{
std::cout << "SomeClass #" << m_id << " deconstructor has been
called." << std::endl;
 
// If the thread is still running, interupt it and wait for it to join
m_thread.interrupt();
m_thread.join();
}
 
//--------------------------------------------------------------------------------------------------
void SomeClass::ThreadProcedure()
{
std::cout << "SomeClass #" << m_id << " thread procedure has been
called." << std::endl;
 
// Will go forever until interupted
try
{
while(true)
{
boost::this_thread::sleep_for(boost::chrono::seconds(60));
std::cout << "SomeClass #" << m_id << " tick..." << std::endl;
}
}
catch(boost::thread_interrupted)
{
std::cout << "SomeClass #" << m_id << " interupted. Exiting
thread." << std::endl;
}
}
 
//--------------------------------------------------------------------------------------------------
 
___________
main.cpp
 
 
// Project Includes
#include "SomeClass.h"
 
// Shared Library
#include "Exception.h"
#include "PerformanceTimer.h"
 
// Boost Includes
#include <boost/thread/thread.hpp>
 
// Standard Library
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
 
#include <signal.h>
 
//--------------------------------------------------------------------------------------------------
// TODO - Only global for concept testing, later we'll wrap all this in
a class
std::vector<SomeClass::Shared_Ptr> g_workerObjects;
boost::thread_group g_workerThreads;
 
//--------------------------------------------------------------------------------------------------
void SignalHandler(int signal)
{
// Signal worker thread to exit
std::cout << "Halt signal caught. Exiting all threads..." << std::endl;
g_workerThreads.interrupt_all();
}
 
//--------------------------------------------------------------------------------------------------
int main()
{
// Register a handler for the ctrl-z signal
signal(SIGINT, SignalHandler);
 
// Create the workers and start thier threads
for(size_t index = 0; index < 10; ++index)
{
SomeClass::Shared_Ptr worker(new SomeClass());
g_workerObjects.push_back(worker);
 
g_workerThreads.add_thread(&(SomeClass::Start(*worker)));
}
 
// Wait for the threads to exit
g_workerThreads.join_all();
 
 
// Done
return 0;
}
 
 
 
 
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
Ian Collins <ian-news@hotmail.com>: Aug 25 10:05AM +1200

Christopher Pisz wrote:
> from the main thread.
 
> The compiler is complaining "error C2280: 'boost::thread::thread(const
> boost::thread &)' : attempting to reference a deleted function"
 
Somewhere you are attempting top copy assign a thread, you can't do that.
 
 
> // Boost Includes
> #include <boost/shared_ptr.hpp>
> #include <boost/thread.hpp>
 
Why mix boost and standard headers?
 
 
--
Ian Collins
Christopher Pisz <nospam@notanaddress.com>: Aug 24 05:13PM -0500

On 8/24/2015 5:05 PM, Ian Collins wrote:
> Christopher Pisz wrote:
SNIP
>> The compiler is complaining "error C2280: 'boost::thread::thread(const
>> boost::thread &)' : attempting to reference a deleted function"
 
> Somewhere you are attempting top copy assign a thread, you can't do that.
 
So you cannot return a boost::thread from a method?
I am not sure why not? Why can't we?
It seems to work if I change it to return a pointer...
 
boost::thread * SomeClass::Start(SomeClass & someClass)
{
// Start the thread and wait for it to exit
someClass.m_thread.swap(boost::thread(someClass.m_threadProcedure));
 
// Return the thread we started, so that the caller may wait on it
to join along with any other
// threads the started from other instances of this class.
return &someClass.m_thread;
}
 
SNIP
 
 
> Why mix boost and standard headers?
 
>> // Standard Includes
>> #include <atomic>
 
Its one of those projects thats being updated from msvc 2010 to 2015. It
already uses boost for threads and posix_time, so don't want to change
too much at once.
 
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
Ian Collins <ian-news@hotmail.com>: Aug 25 10:39AM +1200

Christopher Pisz wrote:
 
>> Somewhere you are attempting top copy assign a thread, you can't do that.
 
> So you cannot return a boost::thread from a method?
> I am not sure why not? Why can't we?
 
You can't copy a thread, doing so doesn't make sense.
 
> It seems to work if I change it to return a pointer...
 
There you aren't doing a copy...
 
--
Ian Collins
Victor Bazarov <v.bazarov@comcast.invalid>: Aug 24 05:53PM -0400

On 8/24/2015 4:51 PM, Thomas Richter wrote:
 
>> In case nobody else answers (and in case that you didn't know), the
>> "throw()" is the exception-specification for that member and it says
>> that the member function does *not* throw any exceptions.
 
My bad. Should have looked. The wording is "the function does *not*
allow any exceptions".
 
> throw(), because the former calls std::terminate() if an exception tries
> to leave the function (i.e. the compiler is not required to unroll the
> stack).
 
Yes, of course "noexcept" (was doing it from memory).
 
Here is what my copy of the C++11 Draft Standard says:
 
<<An exception-specification is
non-throwing if it is of the form throw(), noexcept, or
noexcept(constant-expression ) where the constant-expression
yields true. A function with a non-throwing exception-specification
does not allow any exceptions.>>
 
V
--
I do not respond to top-posted replies, please don't ask
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: