希望同时多次调用函数.我希望使用线程来调用一个能充分利用机器功能的功能.这是一台8核机器,我的要求是使用10%到100%或更高的机器CPU.
我的要求是使用boost类.有什么方法可以使用boost线程或线程池库来完成这个任务吗?还是其他一些方法呢?
另外,如果每次必须使用不同的参数调用多个函数(使用单独的线程),最好的方法是什么?[使用提升或不使用提升]以及如何?
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
using boost::mutex;
using boost::thread;
int threadedAPI1( );
int threadedAPI2( );
int threadedAPI3( );
int threadedAPI4( );
int threadedAPI1( ) {
cout << "Thread0" << endl;
}
int threadedAPI2( ) {
cout << "Thread1" << endl;
}
int threadedAPI3( ) {
cout << "Thread2" << endl;
}
int threadedAPI4( ) {
cout << "Thread3" << endl;
}
int main(int argc, char* argv[]) { …Run Code Online (Sandbox Code Playgroud) 你能告诉我下面的boost :: thread程序有什么问题吗?
#include<iostream>
#include<boost/thread/thread.hpp>
boost::mutex mutex;
class A
{
public:
A() : a(0) {}
void operator()()
{
boost::mutex::scoped_lock lock(mutex);
}
private:
int a;
Run Code Online (Sandbox Code Playgroud)
};
int main()
{
boost::thread thr1(A());
boost::thread thr2(A());
thr1.join();
thr2.join();
Run Code Online (Sandbox Code Playgroud)
}
我收到错误消息:错误:请求'thr1'中的成员'join',这是非类型类型'boost :: thread()(A()())'BoostThread2.cpp:30:错误:请求对于'thr2'中的成员'join',它是非类型的'boost :: thread()(A()())'
我正在尝试用boost库学习一些东西,但是当我尝试编译包含boost :: threads的东西时,我遇到了问题.我在链接期间收到错误,这是消息:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lboost-thread
Run Code Online (Sandbox Code Playgroud)
但这很奇怪,因为只有当我使用普通用户编译时才会发生这种情况,使用root我可以编译没有问题.
提前致谢.
为了学习boost :: thread的组合,我正在为线程实现一个简单的屏障(BR)来锁定一个普通的互斥锁(M).但是,就我转到BR.wait()而言,互斥锁上的锁定没有释放,因此为了让所有线程都到达BR,需要手动释放M上的锁定.所以我有以下代码:
boost::barrier BR(3);
boost::mutex M;
void THfoo(int m){
cout<<"TH"<<m<<" started and attempts locking M\n";
boost::lock_guard<boost::mutex> ownlock(M);
cout<<"TH"<<m<<" locked mutex\n";
Wait_(15); //simple wait for few milliseconds
M.unlock(); //probably bad idea
//boost::lock_guard<boost::mutex> ~ownlock(M);
// this TH needs to unlock the mutex before going to barrier BR
cout<<"TH"<<m<<" unlocked mutex\n";
cout<<"TH"<<m<<" going to BR\n";
BR.wait();
cout<<"TH"<<m<<" let loose from BR\n";
}
int main()
{
boost::thread TH1(THfoo,1);
boost::thread TH2(THfoo,2);
boost::thread TH3(THfoo,3);
TH2.join(); //but TH2 might end before TH1, and so destroy BR …Run Code Online (Sandbox Code Playgroud) 如果我需要处理器内核100%运行十几秒,或者如果我让系统决定如何处理线程,我会获得更好的性能,这样做是否是个好主意?
我需要的是快速执行,我担心系统可能会在使用所有内核之前花费几秒钟,但我还没有找到任何方法来使用boost线程.
我目前有两个线程是生产者和消费者.生成器是一种静态方法,它在Deque类型的静态容器中插入数据,并通过boost::condition_variable在deque对象中插入对象来通知使用者.然后,使用者从Deque类型中读取数据并将其从容器中删除.两个线程使用进行通信boost::condition_variable
这是正在发生的事情的摘要.这是消费者和生产者的代码
//Static Method : This is the producer. Different classes add data to the container using this method
void C::Add_Data(obj a)
{
try
{
int a = MyContainer.size();
UpdateTextBoxA("Current Size is " + a);
UpdateTextBoxB("Running");
MyContainer.push_back(a);
condition_consumer.notify_one(); //This condition is static member
UpdateTextBoxB("Stopped");
}
catch (std::exception& e)
{
std::string err = e.what();
}
}//end method
//Consumer Method - Runs in a separate independent thread
void C::Read_Data()
{
while(true)
{
boost::mutex::scoped_lock lock(mutex_c);
while(MyContainer.size()!=0)
{
try
{
obj …Run Code Online (Sandbox Code Playgroud) 在我的计算机上,在Windows 7上运行,以下代码在Visual C++ 2010中使用Boost 1.53编译,输出
no timeout
elapsed time (ms): 1000
Run Code Online (Sandbox Code Playgroud)
使用GCC 4.8 (在线链接)输出编译的相同代码
timeout
elapsed time (ms): 1000
Run Code Online (Sandbox Code Playgroud)
我的观点是VC++输出不正确,应该是timeout.有没有人no timeout在VC++中有相同的输出(即)?如果是,那么它是Win32实现中的一个错误boost::condition_variable吗?
代码是
#include <boost/thread.hpp>
#include <iostream>
int main(void) {
boost::condition_variable cv;
boost::mutex mx;
boost::unique_lock<decltype(mx)> lck(mx);
boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
const auto cv_res = cv.wait_for(lck, boost::chrono::milliseconds(1000));
boost::chrono::system_clock::time_point end = boost::chrono::system_clock::now();
const auto count = (boost::chrono::duration_cast<boost::chrono::milliseconds>(end - start)).count();
const std::string str = (cv_res == boost::cv_status::no_timeout) ? "no timeout" : "timeout";
std::cout << str << std::endl; …Run Code Online (Sandbox Code Playgroud) 如何阻止一个boost线程并将其从另一个线程中唤醒?线程正在做一些工作,如果工作完成它应该阻塞或睡眠,如果新工作准备好主线程应该弱工作线程.我尝试使用boost ipc message_queue上的阻塞读取,但它不是一个高性能的解决方案.
像这样的东西:
void thread()
{
uint8_t ret=0;
for(;;) //working loop
{
ret=doWork();
if(ret==WORK_COMPLETE)
{
BlockOrSleep();
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用pthreads我可以阻塞信号量,但这不是平台独立的.
我要求在退出std :: thread时执行回调函数,并且应该在主线程上执行回调函数.
在线程创建时,我需要分离线程,并且不能阻止主循环执行以完成线程.
我尝试使用std :: signal,但似乎没有在主线程上执行回调函数
#include <thread>
#include <csignal>
#include <iostream>
std::thread::id main_thread_id;
void func2()
{
for(int i = 0; i < 10000000; i++)
{
// do something
}
}
void func()
{
for(int i = 0; i < 10; i++)
{
func2();
}
std::raise(SIGUSR1);
}
void callback(int signal)
{
std::cout << "SIGNAL: " << signal << " THREAD ID:" <<
std::this_thread::get_id() << std::endl;
bool b = std::this_thread::get_id() == main_thread_id;
std::cout << "IS EXECUTED ON MAIN THREAD: " …Run Code Online (Sandbox Code Playgroud) 我想制作 futures 容器,每个 future 都是一个任务的无效结果,这样我就可以在容器上使用 wait_for_any ,每个任务都是我当前使用 Yield_context 实现的协程,并且在这个协程内部有一个启动函数,它返回 ec 和结果,其中我使用 ec 来分析结果。然后调用另一个协程并传递相同的 Yield_context 。
我想知道如何进行这个设计。
如果我将使用 use_future ,我如何将错误代码传递给 ec 而不抛出它,除非除了抛出它之外没有其他办法,在这种情况下,我将在异步启动函数周围放置 try 和 catch 。
所有这些任务都将在 asio io_service 上发布、生成...。
这是我的代码的主要部分:
这是任务的产生
boost::asio::spawn(GetServiceReference(), boost::bind(&HTTPRequest::Execute, boost::placeholders::_1, m_HttpClient_request_name, Get_mHTTPClient_Responses_Map()));
Run Code Online (Sandbox Code Playgroud)
这是使用yield_context的协程
void HTTPRequest::Execute(boost::asio::yield_context yield_r, std::string request_name, std::map<std::string, boost::shared_ptr<HTTPResponse>>& mHTTPClient_Responses_Map)
{
resolver_iterator iterator_connect = boost::asio::async_connect(mSock, iterator_resolve, yield_r[ec]);
}
Run Code Online (Sandbox Code Playgroud)
而在 Execute 里面我们使用 ec 来分析
if (ec == boost::system::errc::errc_t::success){}
Run Code Online (Sandbox Code Playgroud)
在这里我们启动另一个协程传递相同的yield_context
SendRequest(yield_r);
}
Run Code Online (Sandbox Code Playgroud)
我想改变这一点,这样我就有了所有生成的执行的未来容器,我不关心执行的结果,因为我将它们放入成员类响应中。
但我将来需要结果,以便我可以在容器上使用 wait_any 。
boost-thread ×10
c++ ×9
boost ×4
affinity ×1
boost-asio ×1
boost-bind ×1
boost-mutex ×1
c++11 ×1
future ×1
linker ×1
pthreads ×1