Skip to content

Commit a9a313f

Browse files
author
Herbert Koelman
committed
Merge branch 'iss-28' into develop
2 parents 6d77717 + 4cc8f24 commit a9a313f

File tree

6 files changed

+100
-94
lines changed

6 files changed

+100
-94
lines changed

include/pthread/config.h.in

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,8 @@
2727

2828
#ifdef __IBMCPP__
2929

30-
#define __NOEXCEPT__ throw()
31-
#define __OVERRIDE__
32-
3330
#else
3431

35-
#define __NOEXCEPT__ noexcept
36-
#define __OVERRIDE__ override
37-
3832
#endif
3933

4034
#undef CPP_PTHREAD_VERSION

include/pthread/pthread_exception.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ namespace pthread {
3232
virtual ~pthread_exception(){};
3333

3434
/** @return the exception's error message. */
35-
virtual const char *what() const __NOEXCEPT__ __OVERRIDE__ { return _message.c_str();};
36-
35+
#if __cplusplus < 201103L
36+
virtual const char *what() const throw() { return _message.c_str();};
37+
#else
38+
virtual const char *what() const noexcept override{ return _message.c_str();};
39+
#endif
3740
/** @return pthread error code that was at the orgin of the error */
3841
virtual int pthread_errno(){ return _pthread_errno ;};
3942

include/pthread/thread.hpp

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ namespace pthread {
4848
/**
4949
* This method must be overriden
5050
*/
51-
virtual void run () __NOEXCEPT__ = 0 ;
51+
#if __cplusplus < 201103L
52+
virtual void run () throw() = 0 ;
53+
#else
54+
virtual void run () noexcept = 0 ;
55+
#endif
5256
};
5357

5458
/**
@@ -166,57 +170,57 @@ namespace pthread {
166170
*
167171
* utility class, that wraps a thread.
168172
* <pre><code>
169-
class worker: public pthread::abstract_thread {
170-
public:
171-
172-
worker(const std::string m = "anonymous worker", int sleep = 2*1000): msg(m), _sleep(sleep){
173-
};
174-
175-
~worker(){
176-
};
177-
178-
void run() __NOEXCEPT__ __OVERRIDE__ {
179-
{ // critical section scope
180-
pthread::lock_guard<pthread::mutex> lck(mtx);
181-
182-
bool stop_waiting = true; // if lambda syntax is not availbale then use this kind of implementation
183-
auto delay = _sleep; // use sleep seconds to calculate point in time timeout
184-
while ( ! (stop_waiting = (counter >= 10000)) && (condition.wait_for(mtx, delay) == pthread::cv_status::no_timeout)){
185-
delay = -1 ; // if timeout millis is negatif, then we keep last timeout calculation.
186-
}
187-
188-
if ( counter >= 10000 ) {
189-
message("worker class, counter >= 10000");
190-
} else {
191-
message("worker class, counter < 10000");
192-
}
193-
} // end of critical section
194-
195-
pthread::this_thread::sleep(200);
196-
};
197-
198-
private:
199-
std::string msg ;
200-
int _sleep;
201-
};
202-
203-
int main(int argc, const char * argv[]) {
204-
205-
pthread::thread_group threads(true); // indicate that we want to join referenced threads when deallocating this instance.
206-
for (auto x = 10 ; x > 0 ; x--){
207-
threads.add( new worker("herbert"));
208-
}
209-
210-
threads.start(); // start running all threads
211-
212-
for ( auto x = 20000 ; x > 0 ; x--){
213-
pthread::lock_guard<pthread::mutex> lck(mtx);
214-
counter++ ;
215-
}
216-
217-
condition.notify_all();
218-
}
219-
173+
* class worker: public pthread::abstract_thread {
174+
* public:
175+
*
176+
* worker(const std::string m = "anonymous worker", int sleep = 2*1000): msg(m), _sleep(sleep){
177+
* };
178+
*
179+
* ~worker(){
180+
* };
181+
*
182+
* void run() noexcept override {
183+
* { // critical section scope
184+
* pthread::lock_guard<pthread::mutex> lck(mtx);
185+
*
186+
* bool stop_waiting = true; // if lambda syntax is not availbale then use this kind of implementation
187+
* auto delay = _sleep; // use sleep seconds to calculate point in time timeout
188+
* while ( ! (stop_waiting = (counter >= 10000)) && (condition.wait_for(mtx, delay) == pthread::cv_status::no_timeout)){
189+
* delay = -1 ; // if timeout millis is negatif, then we keep last timeout calculation.
190+
* }
191+
*
192+
* if ( counter >= 10000 ) {
193+
* message("worker class, counter >= 10000");
194+
* } else {
195+
* message("worker class, counter < 10000");
196+
* }
197+
* } // end of critical section
198+
*
199+
* pthread::this_thread::sleep(200);
200+
* };
201+
*
202+
* private:
203+
* std::string msg ;
204+
* int _sleep;
205+
* };
206+
*
207+
* int main(int argc, const char * argv[]) {
208+
*
209+
* pthread::thread_group threads(true); // indicate that we want to join referenced threads when deallocating this instance.
210+
* for (auto x = 10 ; x > 0 ; x--){
211+
* threads.add( new worker("herbert"));
212+
* }
213+
*
214+
* threads.start(); // start running all threads
215+
*
216+
* for ( auto x = 20000 ; x > 0 ; x--){
217+
* pthread::lock_guard<pthread::mutex> lck(mtx);
218+
* counter++ ;
219+
* }
220+
*
221+
* condition.notify_all();
222+
* }
223+
*
220224
* </code></pre>
221225
*/
222226
class abstract_thread: public runnable {
@@ -243,19 +247,19 @@ namespace pthread {
243247
*
244248
* **A thread_group deletes the thread that were registered/added to it.**
245249
*
246-
<pre><code>
247-
int main(int argc, const char * argv[]) {
248-
249-
pthread::thread_group threads; // this instance will free any registered thread when it will get out of scope
250-
251-
for (auto x = 10 ; x > 0 ; x--){
252-
threads.add( new worker("herbert")); // register threads, they will run when start() is called
253-
}
254-
255-
threads.start(); // start running all threads
256-
threads.join(); // wait for registered threads to join
257-
} // scope end
258-
250+
* <pre><code>
251+
* int main(int argc, const char * argv[]) {
252+
*
253+
* pthread::thread_group threads; // this instance will free any registered thread when it will get out of scope
254+
*
255+
* for (auto x = 10 ; x > 0 ; x--){
256+
* threads.add( new worker("herbert")); // register threads, they will run when start() is called
257+
* }
258+
*
259+
* threads.start(); // start running all threads
260+
* threads.join(); // wait for registered threads to join
261+
* } // scope end
262+
*
259263
* </code></pre>
260264
*/
261265
class thread_group{
@@ -264,7 +268,11 @@ namespace pthread {
264268
*
265269
* @param destructor_joins_first if true then destructor tries to wait for all registered threads to join the calling one before deleting thread instances.
266270
*/
267-
thread_group( bool destructor_joins_first = false ) __NOEXCEPT__;
271+
#if __cplusplus < 201103L
272+
thread_group( bool destructor_joins_first = false ) throw();
273+
#else
274+
thread_group( bool destructor_joins_first = false ) noexcept;
275+
#endif
268276

269277
/** delete all abstract_thread referenced by the thread_group.
270278
*

src/condition_variable.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ namespace pthread {
4848
return status;
4949
}
5050

51-
void condition_variable::notify_one() __NOEXCEPT__ {
51+
#if __cplusplus < 201103L
52+
void condition_variable::notify_one() throw() {
53+
#else
54+
void condition_variable::notify_one() noexcept {
55+
#endif
5256
pthread_cond_signal ( &_condition );
5357
}
5458

55-
void condition_variable::notify_all () __NOEXCEPT__{
59+
#if __cplusplus < 201103L
60+
void condition_variable::notify_all () throw(){
61+
#else
62+
void condition_variable::notify_all () noexcept{
63+
#endif
5664
pthread_cond_broadcast ( &_condition );
5765

5866
}

src/thread.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ namespace pthread {
113113
_thread = new pthread::thread(*this);
114114
}
115115

116-
thread_group::thread_group(bool destructor_joins_first ) __NOEXCEPT__: _destructor_joins_first(destructor_joins_first){
116+
#if __cplusplus < 201103L
117+
thread_group::thread_group(bool destructor_joins_first ) throw(): _destructor_joins_first(destructor_joins_first){
118+
#else
119+
thread_group::thread_group(bool destructor_joins_first ) noexcept: _destructor_joins_first(destructor_joins_first){
120+
#endif
117121

118122
}
119123

tests/without-cpp11-pthread-tests.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <utility>
1414
#include <memory>
1515
#include "pthread/pthread.hpp"
16-
// workaround - #include "ibm.hpp"
1716

1817
pthread::condition_variable condition;
1918
pthread::mutex mtx;
@@ -36,7 +35,11 @@ class worker: public pthread::abstract_thread {
3635
message("deallocating worker");
3736
};
3837

39-
void run() __NOEXCEPT__ __OVERRIDE__ {
38+
#if __cplusplus < 201103L
39+
void run() throw() {
40+
#else
41+
void run() noexcept override {
42+
#endif
4043
message("worker: " + msg);
4144
{
4245
pthread::lock_guard<pthread::mutex> lck(mtx);
@@ -70,20 +73,6 @@ int main(int argc, const char * argv[]) {
7073

7174
pthread::string dummy;
7275

73-
{
74-
worker w1("herbert", 2);
75-
w1.start();
76-
w1.join();
77-
}
78-
// worker w("laura");
79-
// pthread::thread t0;
80-
// pthread::thread t2(w);
81-
// t0 = ibm::move(t2) ;
82-
// t0.join();
83-
//
84-
// std::auto_ptr<pthread::thread> pt0(new pthread::thread(worker("laura")));
85-
// pt0->join();
86-
8776
pthread::thread_group threads(true);
8877
for (auto x = 10 ; x > 0 ; x--){
8978
threads.add( new worker("herbert"));
@@ -98,7 +87,7 @@ int main(int argc, const char * argv[]) {
9887
}
9988
condition.notify_all();
10089

101-
// message("main is waiting for threads to finish");
90+
message("main is waiting for threads to finish");
10291
threads.join();
10392
message( "end reached");
10493

0 commit comments

Comments
 (0)