有没有办法在访问时自动锁定STL容器,而不必锁定和释放它?
我有一个在几个项目之间共享的类,它的一些用途是单线程的,有些是多线程的。单线程用户不想要互斥锁的开销,多线程用户不想自己做锁,希望能够有选择地运行在“单线程模式”。所以我希望能够在运行时在真实和“虚拟”互斥体之间进行选择。
理想情况下,我会shared_ptr<something>分配一个真实或虚假的互斥对象。然后我会“锁定”它而不考虑其中的内容。
unique_lock<something> guard(*mutex);
... critical section ...
Run Code Online (Sandbox Code Playgroud)
现在有一个signals2::dummy_mutex但它不与boost::mutex.
那么,在不使锁定/保护代码比上面的示例更复杂的情况下,在真实互斥锁和虚拟互斥锁(signals2 中的互斥锁或其他)之间进行选择的优雅方法是什么?
而且,在您指出替代方案之前:
我正在实现一个消息传递算法.消息通过图形的节点传播,阻塞直到它们已经从(其他邻居)接收到足够的信息来发送消息.
如果我将每条消息放在自己的线程中并使用boost :: condition来暂停线程直到所有必需的信息都可用,那么该算法很容易编写.我创建了数千个线程,但大多数线程在任何时候都是活动的.这看起来效果很好.
我的问题是,当单元测试我发现如果我创建超过大约32705线程,我得到
未知位置(0):"Tree_test"中的致命错误:std :: exception:boost :: thread_resource_error
我不知道是什么原因造成的,或者如何解决这个问题.
似乎有很多可用的内存(每个线程只有两个指针 - 消息在它们之间传递的对象).
从这个问题:Linux中每个进程的最大线程数?我认为以下信息是相关的(虽然我真的不知道它有什么意思......)
~> cat /proc/sys/kernel/threads-max
1000000
Run Code Online (Sandbox Code Playgroud)
(我从60120增加了这个 - 我需要重启吗?)
~>ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 20
file size (blocks, -f) unlimited
pending signals (-i) 16382
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time …Run Code Online (Sandbox Code Playgroud) 我发现在以下简单程序中,boost线程开销有三个数量级的时序开销.反正有没有减少这种开销并加快fooThread()通话速度?
#include <iostream>
#include <time.h>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
typedef uint64_t tick_t;
#define rdtscll(val) do { \
unsigned int __a,__d; \
__asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
(val) = ((unsigned long long)__a) | (((unsigned long long)__d)<<32); \
} while(0)
class baseClass {
public:
void foo(){
//Do nothing
}
void threadFoo(){
threadObjOne = boost::thread(&baseClass::foo, this);
threadObjOne.join();
}
private:
boost::thread threadObjOne;
};
int main(){
std::cout<< "main startup"<<std::endl;
baseClass baseObj;
tick_t startTime,endTime;
rdtscll(startTime);
baseObj.foo();
rdtscll(endTime);
std::cout<<"native foo() call takes …Run Code Online (Sandbox Code Playgroud) 我想对共享变量进行两次操作。我需要保证它可以原子地完成。有人可以帮我澄清以下方法是否正确:
#include <atomic>
std::atomic<int> index;
void function()
{
// I need the variable index to be incremented but bound in the
// range of [0,9].
int loc_indx = index.load(std::memory_order_acquire);
index.store( (loc_indx+1)%10 , std::memory_order_release);
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,索引存储操作和索引加载操作必须一起发生。这里的一些专家可以澄清上面的代码是否等同于以下伪代码:
ATOMIC
{
index = (index+1)%10;
}
Run Code Online (Sandbox Code Playgroud)
我一直在 Visual Studio 2012 的 c++ 部分或/和 1.53 的 boost::atomic 部分中使用原子包。
以下是Boost库文档中给出的代码.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!\n";
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.async_wait(print);
io.run();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在当我运行上面的程序时,它只等待5秒然后打印Hello World并停止.我希望这个程序每5秒钟继续打印Hello World.可能吗 ?
我正在使用PortAudio来实现实时音频处理。
我的主要任务是连续从麦克风获取数据,并提供100个要处理的样本(each FRAME = 100 samples at a time)到其他处理线程。
这是我的回调,每次连续收集100个样本-
static int paStreamCallback( const void* input, void* output,
unsigned long samplesPerFrame,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData ) {
paTestData *data = (paTestData*) userData;
const SAMPLE *readPtr = (const SAMPLE*)input; // Casting input read to valid input type SAMPLE (float)
unsigned long totalSamples = TOTAL_NUM_OF_SAMPLES ; // totalSamples = 100 here
(void) output;
(void) timeInfo;
(void) statusFlags;
static int count = 1;
if(data->sampleIndex < count …Run Code Online (Sandbox Code Playgroud) 我正在更新以前使用自己的读写锁定机制的代码(此代码是在 C++11 之前编写的,并且 std::shared_mutex 尚不存在)以使用 C++ 的 std 版本。
有 ReadLock 和 WriteLock 两个类,可以通过调用方法将 ReadLock 升级为 WriteLock。WriteLock 也可以随时获取,无需从 shared_lock 升级。阅读 shared_lock 的 C++ std 版本,我认为它很简单。
ReadLock 将被 shared_lock 替换 WriteLock 将被 unique_lock 替换并且可以随时触发调用 lock() 来获取 Writelock 升级到 Writelock 将只分两步执行解锁 shared_lock 和锁定 unique_lock
我现在的问题是,当我阅读讨论时,可能会出现特定问题,并且像这样的线程引起了关注(来自 Howard Hinnant 的线程) std::shared_timed_mutex 上的共享锁可以升级为排他锁吗?
所以我考虑使用 boost 版本,因为它支持 upgrade_lock 和 boost::upgrade_to_unique_lock,但我对如何处理它的设计感到困惑。
WriteLock 类可以同时表示 unique_lock 和/或 upgrade_lock 和 upgrade_to_unique_lock 对象,因为正如我上面提到的,WriteLock 也可以在没有 shared_lock 的情况下实现。
另外,如果我使用 boost 中的 upgrade_lock,我对如何显式触发 lock() 和 unlock() 机制感到困惑;boost::upgrade_lock 有一个构造函数,它接受 defer_lock 作为参数,并具有我可以随时使用的 lock() 和 unlock() 方法,但是如果我们使用 …
我是C++中多线程的完全新手,并决定从Boost Libraries开始.另外,我在Vista上使用英特尔的C++编译器(来自Parallel Studio 2011)和VS2010.
我正在编写一个遗传算法,并希望利用多线程的好处:我想为人口中的每个人(对象)创建一个线程,以便他们并行计算他们的适应度(重度操作),减少总执行时间.
据我所知,每当我启动一个子线程时,它就会"在后台"工作,而父线程继续执行下一条指令,对吧?所以,我想到创建并启动我需要的所有子线程(在for循环中),然后等待它们完成(join()在另一个for循环中调用每个线程),然后再继续.
我面临的问题是第一个循环不会继续下一次迭代,直到新创建的线程完成工作.然后,第二个循环就像去了一样好,因为所有的线程都已经被循环命中时加入了.
这是(我认为是)相关的代码片段.告诉我你还有什么需要知道的.
class Poblacion {
// Constructors, destructor and other members
// ...
list<Individuo> _individuos;
void generaInicial() { // This method sets up the initial population.
int i;
// First loop
for(i = 0; i < _tamano_total; i++) {
Individuo nuevo(true);
nuevo.Start(); // Create and launch new thread
_individuos.push_back(nuevo);
}
// Second loop
list<Individuo>::iterator it;
for(it = _individuos.begin(); it != _individuos.end(); it++) {
it->Join();
}
_individuos.sort();
}
}; …Run Code Online (Sandbox Code Playgroud) 我有一个小程序,使用各种卡计数策略实现BlackJack的蒙特卡罗模拟.我的主要功能基本上是这样的:
int bankroll = 50000;
int hands = 100;
int tests = 10000;
Simulation::strategy = hi_lo;
for(int i = 0; i < simulations; ++i)
runSimulation(bankroll, hands, tests, strategy);
Run Code Online (Sandbox Code Playgroud)
整个程序在我的机器上的单个线程中运行大约需要10秒钟.
我想利用我的处理器所拥有的3个内核,所以我决定重写程序,只需在不同的线程中执行各种策略,如下所示:
int bankroll = 50000;
int hands = 100;
int tests = 10000;
Simulation::strategy = hi_lo;
boost::thread threads[simulations];
for(int i = 0; i < simulations; ++i)
threads[i] = boost::thread(boost::bind(runSimulation, bankroll, hands, tests, strategy));
for(int i = 0; i < simulations; ++i)
threads[i].join();
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此程序时,即使我得到相同的结果,也需要大约24秒才能完成.我在这里错过了什么吗?
boost-thread ×10
c++ ×9
boost ×3
boost-asio ×1
c ×1
c++11 ×1
concurrency ×1
containers ×1
linux ×1
mutex ×1
performance ×1
portaudio ×1
stl ×1
threadpool ×1
visual-c++ ×1