Home > Back-end >  How about those thread lock right
How about those thread lock right

Time:11-28

I wrote a thread pool:
This is the code:
 
# # ifndef THREAD_POOL_H
# define THREAD_POOL_H
The namespace thread_pools {
Bool have_to_stop=false;
Struct function_to_functional {
templateFunctionOperator () (return_type_is fun (arguments_type_is... ))
{
FunctionReturn the p;
}
};
The 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 (). The join ();
Thread_work. Erase (thread_work. Begin () + thread_work. The size () - 1);
}
The else
Throw logic_error (" empty_thread_pool ");
}
Private:
Vector Thread_work;
Mutex lock_on;
Condition_variable condition_on;
templateVoid add_new_work (const function& This_function arguments_type... Arguments_are)
{
Thread_work. Push_back (thread (this_function arguments_are... ));
}
templateVoid change_work (const functionOther_arguments_type... ) & gt; & Other_function other_arguments_type...
Other_arguments, const size_t & amp; Where_is_it)
{
Thread_work] [where_is_it - 1=thread (other_function other_arguments... );
}
Void delete_work (const size_t & amp; Which_delete)
{
Thread_work [which_delete]. The join ();
Thread_work. Erase (thread_work. The begin () + which_delete - 1);
}
Public:
Thread_pool ()
{
New_size (10);
For (auto& P: thread_work)
P=thread ();
}
Thread_pool (const size_t & amp; Basic_size)
{
New_size (basic_size);
For (auto& P: thread_work)
P=thread ();
}
Size_t get_pool_size ()
{
Return thread_work. The size ();
}
Void out_last_thread ()
{
Unique_lock & lt; 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;
}
templateVoid push_back_thread (const function. ) & gt; & Function_p type_argument... Real_argument)
{
Add_new_work (function_p real_argument... );
}
templateVoid change_thread (const functionChange_type_argument... ) & gt; & Change_function change_type_argument...
Change_real_argument, const size_t & amp; Change_position)
{
Change_work (change_function change_real_argument... ,
Change_position);
}
templateVoid push_back_thread (type_returns func (type_arguments... ),
Type_arguments... Real_arguments)
{
Function_to_functional p;
Add_new_work (p (func), real_arguments... );
}
templateVoid change_thread (change_type_returns func (change_type_arguments... ),
Change_type_arguments... Change_real_arguments, const size_t & amp;
The position)
{
Function_to_functional p;
Change_work (p (func), change_real_arguments... , the position);
}
~ thread_pool ()
{
For (auto& P: thread_work)
P.j oin ();
}
};
}
# endif

But when testing code found still can appear the phenomenon of "competition" (and a thread has not been performed, another thread has already performed)
This is the test code:
 
#include
# include "thread_pool. H"
using namespace std;
Using the namespace thread_pools;
Void func (int id)
{
cout<" Id: "& lt; }
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;
}

I now how to modify out_last_thread function?
Thank you all for the great god ~ ~ ~

CodePudding user response:

The key to just see out_last_thread function

CodePudding user response:

This is how to return a responsibility excuse me?

CodePudding user response:

Thread object once created is automatically started to run time, you don't lock and condition variables like the start of the control, like control over, but the condition variable nor do ah, so many blogs or to look good,
Condition variables have no thread to notify at the beginning, all is to wait first, seemingly card die,

CodePudding user response:

reference SDGHCHJ reply: 3/f
thread object once created is automatically started to run time, you don't lock and condition variables like the start of the control, like control over, but the condition variable nor do ah, so many blogs or to look good,
Condition variables have no thread to notify at the beginning, all is to wait first, seemingly card die,

I'm phenomenon such as the output is:
Id: id 7 8
9 id:
And so on,
How do I modify?
Add the lock at the front?
  • Related