Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[0a1223]: mt / Thread.cpp Maximize Restore History

Download this file

Thread.cpp    99 lines (87 with data), 2.9 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
*
* Thread.cpp -- wrapper class for making methods asynchronously callable
*
* \file mt/Thread.cpp
*
* Thanks to Carlos O'Ryan (coryan@cs.wustl.edu) for elucidating
* the Daemon_Adapter pattern on which this is closely based.
*
* \author Oliver M. Kellogg <Oliver.Kellogg@vs.dasa.de> (initial)
* \author Thomas Eschenbacher <Thomas.Eschenbacher@gmx.de> (ported to Kwave)
* \date 1998-08-12 (initial)
* \date 2000-09-30 (ported to the Kwave project)
*
* \par NOTE:
* this code is taken from the following news article
* (found by www.dejanews.com):
*
* \verbatim
* ----------------------------------------------------------------------------
* Author: Oliver M. Kellogg <Oliver.Kellogg@vs.dasa.de>
* Date: 1998/08/12
* Forums: comp.soft-sys.ace
* ----------------------------------------------------------------------------
* Subject: Re: [ace-users] Handing non-static methods into ACE_Thread::spawn()
* From: ace-users@cs.wustl.edu
* Date: 1998/08/12
* Newsgroups: comp.soft-sys.ace
* ----------------------------------------------------------------------------
* \endverbatim
*
* \bug C++ exception handling does not work across additionally spawned
* client threads of this daemon.
*
* \bug C signal handling is not done, a setlongjmp environment has to
* be set up.
*/
#include "config.h"
#include <errno.h>
#include "mt/TSS_Object.h"
#include "mt/Thread.h"
/**
* C wrapper function for Thread::thread_adapter.
* @internal
*/
extern "C" void* C_thread_adapter(void* arg)
{
Thread *thread = (Thread *)arg;
if (!thread) return 0;
/* call the daemon's function */
void* result = thread->thread_adapter(arg);
return result;
}
//***************************************************************************
Thread::Thread(int *grpid, const long flags)
:TSS_Object(), m_tid(0)
{
pthread_attr_init(&m_attr);
pthread_attr_setdetachstate(&m_attr, PTHREAD_CREATE_DETACHED);
}
//***************************************************************************
Thread::~Thread()
{
pthread_attr_destroy(&m_attr);
}
//***************************************************************************
void *Thread::thread_adapter(void *arg)
{
Thread *object = (Thread *) arg;
if (!object) return (void*)-EINVAL;
/* execute the thread function */
object->run();
return arg;
}
//***************************************************************************
void Thread::start()
{
pthread_create(&m_tid, &m_attr, C_thread_adapter, this);
}
//***************************************************************************
void Thread::stop()
{
pthread_cancel(m_tid);
}
//***************************************************************************
//***************************************************************************
/* end of Thread.cpp */