2
0
mirror of https://github.com/boostorg/thread.git synced 2026-01-22 17:52:18 +00:00

Modified to use Boost.Function for the threadproc.

[SVN r10437]
This commit is contained in:
William E. Kempf
2001-06-26 19:20:11 +00:00
parent 1d6f10702d
commit 2febaf386c
5 changed files with 52 additions and 24 deletions

View File

@@ -48,7 +48,7 @@ public:
return buf;
}
static void do_sender_thread(void*)
static void do_sender_thread()
{
for (int n = 0; n < ITERS; ++n)
{
@@ -60,7 +60,7 @@ public:
}
}
static void do_receiver_thread(void*)
static void do_receiver_thread()
{
int n;
do
@@ -83,8 +83,8 @@ void do_test(M* dummy=0)
{
typedef buffer_t<M> buffer_type;
buffer_type::get_buffer();
boost::thread::create(&buffer_type::do_sender_thread, 0);
boost::thread::create(&buffer_type::do_receiver_thread, 0);
boost::thread::create(&buffer_type::do_sender_thread);
boost::thread::create(&buffer_type::do_receiver_thread);
boost::thread::join_all();
}

View File

@@ -63,7 +63,7 @@ private:
canteen g_canteen;
void chef(void*)
void chef()
{
const int chickens = 4;
{
@@ -139,15 +139,25 @@ struct thread_adapt
void* _param;
};
class thread_adapter
{
public:
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
void operator()() const { _func(_param); }
private:
void (*_func)(void*);
void* _param;
};
int main(int argc, char* argv[])
{
boost::thread::create(&chef, 0);
boost::thread::create(&chef);
phil p[] = { phil(0), phil(1), phil(2), phil(3), phil(4) };
boost::thread::create(&phil::do_thread, &p[0]);
boost::thread::create(&phil::do_thread, &p[1]);
boost::thread::create(&phil::do_thread, &p[2]);
boost::thread::create(&phil::do_thread, &p[3]);
boost::thread::create(&phil::do_thread, &p[4]);
boost::thread::create(thread_adapter(&phil::do_thread, &p[0]));
boost::thread::create(thread_adapter(&phil::do_thread, &p[1]));
boost::thread::create(thread_adapter(&phil::do_thread, &p[2]));
boost::thread::create(thread_adapter(&phil::do_thread, &p[3]));
boost::thread::create(thread_adapter(&phil::do_thread, &p[4]));
boost::thread::join_all();
return 0;
}

View File

@@ -67,12 +67,22 @@ struct thread_adapt
void* _param;
};
class thread_adapter
{
public:
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
void operator()() const { _func(_param); }
private:
void (*_func)(void*);
void* _param;
};
int main(int argc, char* argv[])
{
state = START;
boost::thread::create(&player, (void*)PLAYER_A);
boost::thread::create(&player, (void*)PLAYER_B);
boost::thread::create(thread_adapter(&player, (void*)PLAYER_A));
boost::thread::create(thread_adapter(&player, (void*)PLAYER_B));
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);

View File

@@ -97,7 +97,7 @@ namespace boost
static void* thread_proc(void* param);
#endif
static thread_state* create(const boost::detail::threadfunc& func, void* param);
static thread_state* create(const boost::detail::threadfunc& func);
private:
unsigned long _refs;
@@ -105,7 +105,6 @@ namespace boost
condition _cond;
int _state;
threadfunc _func;
void* _param;
#if defined(BOOST_HAS_WINTHREADS)
HANDLE _thread;
#elif defined(BOOST_HAS_PTHREADS)
@@ -188,7 +187,7 @@ namespace boost
try
{
state->_func(state->_param);
state->_func();
}
catch (...)
{
@@ -206,14 +205,13 @@ namespace boost
return 0;
}
thread_state* thread_state::create(const boost::detail::threadfunc& func, void* param)
thread_state* thread_state::create(const boost::detail::threadfunc& func)
{
thread_state* state = new thread_state();
mutex::lock lock(state->_mutex);
assert(func);
state->_func = func;
state->_param = param;
#if defined(BOOST_HAS_WINTHREADS)
unsigned id;
@@ -276,10 +274,10 @@ namespace boost
_state->join();
}
thread thread::create(const detail::threadfunc& func, void* param)
thread thread::create(const detail::threadfunc& func)
{
thread temp;
temp._state = detail::thread_state::create(func, param);
temp._state = detail::thread_state::create(func);
return temp;
}

View File

@@ -210,11 +210,21 @@ void condition_test_thread(void* param)
data->awoken++;
}
class thread_adapter
{
public:
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
void operator()() const { _func(_param); }
private:
void (*_func)(void*);
void* _param;
};
void test_condition_notify_one()
{
condition_test_data data;
boost::thread::create(&condition_test_thread, &data);
boost::thread::create(thread_adapter(&condition_test_thread, &data));
{
boost::mutex::lock lock(data.mutex);
@@ -233,7 +243,7 @@ void test_condition_notify_all()
condition_test_data data;
for (int i = 0; i < NUMTHREADS; ++i)
boost::thread::create(&condition_test_thread, &data);
boost::thread::create(thread_adapter(&condition_test_thread, &data));
{
boost::mutex::lock lock(data.mutex);
@@ -302,7 +312,7 @@ void test_condition_waits()
{
condition_test_data data;
boost::thread::create(&condition_test_waits, &data);
boost::thread::create(thread_adapter(&condition_test_waits, &data));
boost::xtime xt;
@@ -390,7 +400,7 @@ void test_semaphore()
BOOST_TEST(boost::read(atomic) == 20);
}*/
void test_tss_thread(void*)
void test_tss_thread()
{
static boost::tss value;
value.set(new int(0));