我自己寫了個執行緒池:
這是代碼:
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
namespace thread_pools{
bool have_to_stop=false;
struct function_to_functional{
template<class return_type_is,class... arguments_type_is>
function<return_type_is(arguments_type_is...)>
operator()(return_type_is fun(arguments_type_is...))
{
function<return_type_is(arguments_type_is...)> p=fun;
return p;
}
};
class thread_pool{
protected:
size_t size_now;
void new_size(const unsigned int& new_size_is)
{
thread_work.reserve(new_size_is);
}
void destory()
{
if(!thread_work.empty())
{
thread_work.back().join();
thread_work.erase(thread_work.begin()+thread_work.size()-1);
}
else
throw logic_error("empty_thread_pool");
}
private:
vector<thread> thread_work;
mutex lock_on;
condition_variable condition_on;
template<class return_type,class... arguments_type>
void add_new_work(const function<return_type(arguments_type...)>
& this_function,arguments_type... arguments_are)
{
thread_work.push_back(thread(this_function,arguments_are...));
}
template<class other_return_type,class... other_arguments_type>
void change_work(const function<other_return_type(
other_arguments_type...)>& other_function,other_arguments_type...
other_arguments,const size_t& where_is_it)
{
thread_work[where_is_it-1]=thread(other_function,other_arguments...);
}
void delete_work(const size_t& which_delete)
{
thread_work[which_delete].join();
thread_work.erase(thread_work.begin()+which_delete-1);
}
public:
thread_pool()
{
new_size(10);
for(auto& p:thread_work)
p=thread();
}
thread_pool(const size_t& basic_size)
{
new_size(basic_size);
for(auto& p:thread_work)
p=thread();
}
size_t get_pool_size()
{
return thread_work.size();
}
void out_last_thread()
{
unique_lock<mutex> this_lock(lock_on);
while(have_to_stop==true)
condition_on.wait(this_lock);
have_to_stop=true;
condition_on.notify_one();
have_to_stop=false;
}
template<class type_return,class... type_argument>
void push_back_thread(const function<type_return(type_argument
...)>& function_p,type_argument... real_argument)
{
add_new_work(function_p,real_argument...);
}
template<class change_type_return,class... change_type_argument>
void change_thread(const function<change_type_return(
change_type_argument...)>& change_function,change_type_argument...
change_real_argument,const size_t& change_position)
{
change_work(change_function,change_real_argument...,
change_position);
}
template<class type_returns,class... type_arguments>
void push_back_thread(type_returns func(type_arguments...),
type_arguments... real_arguments)
{
function_to_functional p;
add_new_work(p(func),real_arguments...);
}
template<class change_type_returns,class... change_type_arguments>
void change_thread(change_type_returns func(change_type_arguments...),
change_type_arguments... change_real_arguments,const size_t&
position)
{
function_to_functional p;
change_work(p(func),change_real_arguments...,position);
}
~thread_pool()
{
for(auto& p:thread_work)
p.join();
}
};
}
#endif
但是在測驗代碼時發現任然會出現“競爭”的現象(及一個執行緒還沒有執行完,另一個執行緒卻已經開始執行了)
這是測驗代碼:
#include<iostream>
#include"thread_pool.h"
using namespace std;
using namespace thread_pools;
void func(int id)
{
cout<<"id:"<<id<<endl;
}
int main()
{
thread_pool test;
for(int i=0;i<=9;++i)
test.push_back_thread(func,i);
for(int i=0;i<=9;++i)
test.out_last_thread();
return 0;
}
我現在該如何修改out_last_thread 函式呢?
謝謝各位大神~~~
uj5u.com熱心網友回復:
關鍵只要看out_last_thread函式就行了uj5u.com熱心網友回復:
請問這到底是怎么回事?uj5u.com熱心網友回復:
執行緒物件一旦創建就自動開始運行時了,你的鎖和條件變數不像是在控制開始,倒像是控制結束,但條件變數也不是那樣用的呀,那么多博客還是去看看好。條件變數一開始都沒有執行緒notify,全都是先wait,貌似卡死了吧。
uj5u.com熱心網友回復:
我出現的現象是例如輸出是:
id:7 id 8
9id:
等等。
我該如何修改呢?
是不是把鎖加在前面?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/19173.html
標籤:C++ 語言
上一篇:這道c++題咋做啊
下一篇:如何查看專案中使用的c++版本
