我正在学习并发编程,我想要做的是有一个类,每个对象负责运行自己的Boost:Thread.我对这段代码略胜一筹,因为它使用了很多我不太熟悉的功能(动态分配内存,函数指针,并发等等).这就像每行代码我必须检查一些引用才能使它正确.
(是的,在实际代码中考虑了所有分配的内存!)
我遇到了互斥体问题.我声明它是静态的,它似乎为所有实例获得相同的值(应该如此).代码仍然不是线程安全的.
互斥锁应该阻止线程(右边?)进一步前进以防其他人锁定它.因为互斥量是作用域的(一种简洁的功能)并且它在if语句中应该看其他线程没有?我仍然得到控制台输出,明确暗示它不是线程安全的.
此外,我不确定我是否正在使用静态可变权利.我尝试了不同的方式来引用它(Seller :: ticketSaleMutex),但唯一有效的是"this-> ticketSaleMutex",它看起来非常阴暗,似乎打败了静态的目的.
Seller.h:
class Seller
{
public:
//Some vaiables
private:
//Other variables
static boost::mutex ticketSaleMutex; //Mutex definition
};
Run Code Online (Sandbox Code Playgroud)
Seller.cpp:
boost::mutex Seller::ticketSaleMutex; //Mutex declaration
void Seller::StartTicketSale()
{
ticketSale = new boost::thread(boost::bind(&Seller::SellTickets, this));
}
void Seller::SellTickets()
{
while (*totalSoldTickets < totalNumTickets)
{
if ([Some time tick])
{
boost::mutex::scoped_lock(this->ticketSaleMutex);
(*totalSoldTickets)++;
std::cout << "Seller " << ID << " sold ticket " << *totalSoldTickets << std::endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
int main(int argc, char**argv)
{
std::vector<Seller*> seller;
const …Run Code Online (Sandbox Code Playgroud) 我之前正在使用pthread_mutex_ts.代码有时会卡住.我有几行代码分散在我包装的函数中......
pthread_mutex_lock(&map_mutex);// Line 1
//critical code involving reading/writing wrapped around a mutex //Line 2
pthread_mutex_unlock(&map_mutex); //Line 3
Run Code Online (Sandbox Code Playgroud)
不确定代码被卡住的方式/位置,我切换pthread_mutex_t到了boost:mutex
1)如果我只是用第1行替换第1行和第3 boost::lock_guard<boost::mutex> lock(map_mutex);行,并且一切都运行良好,那么pthread实现可能会出现什么问题?
2)我是否通过切换到提升而放弃了性能.这里的关键部分非常时间敏感,所以我希望互斥体非常轻巧.(C++,redhat)
我正在尝试使用C++中的线程和互斥体进行简单的操作.
这是代码:
#include <iostream>
#include <boost/thread/thread.hpp>
class mutex_test
{
private:
boost::mutex mut;
public:
void operator()()
{
boost::mutex::scoped_lock lock(mut);
std::cout << "Hi!" << std::endl;
}
};
int main()
{
mutex_test tester;
boost::thread tester_thread(tester);
tester_thread.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时,我收到此错误:
In file included from C:\boost/boost/thread/detail/thread.hpp:15:0,
from C:\boost/boost/thread/thread.hpp:22,
from main.cpp:3:
C:\boost/boost/thread/detail/move.hpp: In instantiation of 'typename boost::decay<T>::type boost::thread_detail::decay_copy(T&&) [with T = mutex_test&; typename boost::decay<T>::type = mutex_test]':
C:\boost/boost/thread/detail/thread.hpp:265:88: required from 'boost::thread::thread(F&&) [with F = mutex_test&]'
main.cpp:20:36: required from here
C:\boost/boost/thread/detail/move.hpp:246:37: error: use of deleted function 'mutex_test::mutex_test(const …Run Code Online (Sandbox Code Playgroud) 此代码将无法编译:
class MyClass
{
boost::mutex _mutex;
void foo() const
{
boost::mutex::scoped_lock lock(_mutex);
//critical section
}
}
Run Code Online (Sandbox Code Playgroud)
但是将函数定义为非const将正常工作.请问有人可以解释原因吗?谢谢!
我有一个std::unordered_map来自多个线程的非常繁重的工作负载.我可以使用a std::mutex进行同步,但由于并发读取应该没问题,我想用一个boost::shared_mutex代替.为了测试性能改进,我首先使用一堆值预先填充一个映射,然后让一堆线程运行读取测试:
for (int i = 0; i < iters; ++i) map.count(random_uint(0, key_max));
Run Code Online (Sandbox Code Playgroud)
我跑这对我的粗锁的实现,其中count被保护std::lock_guard<std::mutex>和我共享锁实现在那里被保护boost::shared_lock<boost::shared_mutex>.
在我的带有GCC 6.1.1的Arch Linux x86_64系统上,boost::shared_lock版本总是更慢!在我的朋友与MSVC 2013的Windows 10系统中,boost::shared_lock是永远快!
完整的,可编译的代码在github上:https://github.com/silverhammermba/sanity
这似乎是一个特定于平台的问题.往上看.我真的很感激,如果其他人可以构建和运行此代码并报告他们是否看到正输出(shared_lock更快)或负面(当前互斥更快)以及您正在使用的平台.
我正在尝试从子进程中使用 C++ 中的同步队列。我在 C++ () ( http://www.internetmosquito.com/2011/04/making-thread-safe-queue-in-ci.html ) 中使用这个同步队列
我修改了队列是在升压序列化,也取代了使用boost::mutex io_mutex_,而不是使用inteprocess互斥(感谢@Sehe)boost::interprocess::interprocess_mutex io_mutex_和锁定,当我改为每有行boost::mutex::scoped_lock lock(io_mutex_);到scoped_lock<interprocess_mutex> lock(io_mutex_);
template<class T>
class SynchronizedQueue
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & sQueue;
ar & io_mutex_;
ar & waitCondition;
}
... // queue implementation (see [http://www.internetmosquito.com/2011/04/making-thread-safe-queue-in-c-i.html][2])
Run Code Online (Sandbox Code Playgroud)
}
在我的测试应用程序中,我正在创建同步队列并在其中存储此类的 100 个实例:
class gps_position
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees; …Run Code Online (Sandbox Code Playgroud) 我正在尝试Boost线程,因为据我所知,我可以编写一个多线程Boost应用程序并在Windows或Linux中编译它,而pthreads我更熟悉的是,它严格用于*NIX系统.
我有以下示例应用程序,它是从另一个SO问题中借用的:
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>
#define NAP_DURATION (10000UL) // 10ms
boost::mutex io_mutex;
void count(int id)
{
for (int i = 0; i < 1000; ++i)
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << "Thread ID:" << id << ": " << i << std::endl;
if (id == 1)
{
std::cout << "I'm thread " << id << " and I'm taking a short nap" << std::endl;
usleep(NAP_DURATION);
}
else
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用boost 1.54.0和Visual Studio 2010.对于代码:
#include <iostream>
#include "boost/thread/thread.hpp"
#include "boost/thread/mutex.hpp"
boost::mutex mx1;
void func1()
{
{
boost::mutex::scoped_lock(mx1);
std::cout << "Thread " << boost::this_thread::get_id() << " starting work." << std::endl;
}
int x = 0;
for (int i=0; i<100; i++)
x++;
{
boost::mutex::scoped_lock(mx1);
std::cout << "Thread " << boost::this_thread::get_id() << " finished." << std::endl;
}
}
int main(void)
{
boost::thread thread1(&func1);
boost::thread thread2(&func1);
thread1.join();
thread2.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
大约一半的时间我得到以下(显然有不同的线程ID和执行顺序):
Thread Thread 15b0 starting work.
1a18 starting work.
Thread 15b0 finished.
Thread …Run Code Online (Sandbox Code Playgroud) 我正在玩boost库和C++.我想创建一个包含生产者,conumer和堆栈的多线程程序.procuder填充堆栈,消费者从堆栈中删除items(int).一切正常(pop,push,mutex)但是当我把pop/push winthin称为线程时,我没有任何效果
我做了这个简单的代码:
#include "stdafx.h"
#include <stack>
#include <iostream>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <boost/signals2/mutex.hpp>
#include <ctime>
using namespace std;
/ *
* this class reprents a stack which is proteced by mutex
* Pop and push are executed by one thread each time.
*/
class ProtectedStack{
private :
stack<int> m_Stack;
boost::signals2::mutex m;
public :
ProtectedStack(){
}
ProtectedStack(const ProtectedStack & p){
}
void push(int x){
m.lock();
m_Stack.push(x);
m.unlock();
}
void pop(){
m.lock();
//return m_Stack.top();
if(!m_Stack.empty())
m_Stack.pop(); …Run Code Online (Sandbox Code Playgroud) 我已经在这里待了好几天(甚至发布在 boost 论坛上)并且能够让第二个进程识别锁定的互斥锁似乎不起作用。请帮忙。这是代码:
一个常见的头文件:SharedObject.hpp
#ifndef SHAREDOBJECT_HPP
#define SHAREDOBJECT_HPP
#include <iostream>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <time.h>//for sleep
//--------for mutexes
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/sharable_lock.hpp>
#include <boost/interprocess/sync/upgradable_lock.hpp>
#define MUTEX_SHARED_MEMORY_NAME "NavSharedMemoryMutex"
#define DATAOUTPUT "OutputFromObject"
#define INITIAL_MEM 650000
using namespace std;
namespace bip = boost::interprocess;
class SharedMutex
{
private:
typedef bip::interprocess_upgradable_mutex upgradable_mutex_type;
mutable upgradable_mutex_type mutex;
volatile int counter;
public:
void lockWithReadLock() const { bip::sharable_lock<upgradable_mutex_type> lock(mutex); }
void lockWithWriteLock() { bip::scoped_lock<upgradable_mutex_type> lock(mutex); }
};
//-------------------------------------- …Run Code Online (Sandbox Code Playgroud)