Skip to content

Commit 1a3a39c

Browse files
author
Herbert Koelman
committed
refs #41
* pthread attribute becomes a class attribute
1 parent 7e22c4d commit 1a3a39c

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

include/pthread/thread.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ namespace pthread {
167167
void swap ( thread& other );
168168

169169
pthread_t _thread;
170+
pthread_attr_t _attr;
170171

171172
thread_status _status;
172173
};

src/thread.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,24 @@ namespace pthread {
6161

6262
thread::thread (const runnable &work, const std::size_t stack_size ): thread(){ // ": thread()" calls the related anonymous constructor
6363
int rc = 0 ;
64-
pthread_attr_t attr;
6564

6665
/* Initialize and set thread detached attribute */
67-
if ( (rc = pthread_attr_init(&attr)) != 0){
66+
if ( (rc = pthread_attr_init(&_attr)) != 0){
6867
throw thread_exception("pthread_attr_init failed.", rc );
6968
}
7069

71-
if ( (rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) != 0 ){
70+
if ( (rc = pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_JOINABLE)) != 0 ){
7271
throw thread_exception("pthread_attr_setdetachstate failed.", rc );
7372
}
7473

75-
if ( stack_size > 0 && (rc = pthread_attr_setstacksize(&attr, stack_size)) != 0 ){
74+
if ( stack_size > 0 && (rc = pthread_attr_setstacksize(&_attr, stack_size)) != 0 ){
7675
throw thread_exception("pthread_attr_setstacksize failed.", rc );
7776
}
7877

79-
if ((rc = pthread_create(&_thread, &attr, thread_startup_runnable, (void *) &work)) != 0){
78+
if ((rc = pthread_create(&_thread, &_attr, thread_startup_runnable, (void *) &work)) != 0){
8079
throw thread_exception("pthread_create failed.", rc );
8180
} else {
8281
_status = thread_status::a_thread;
83-
pthread_attr_destroy(&attr);
8482
}
8583

8684
}
@@ -91,6 +89,10 @@ namespace pthread {
9189
swap(other);
9290
}
9391

92+
thread::~thread () {
93+
pthread_attr_destroy(&_attr);
94+
}
95+
9496
/* move operator */
9597
thread& thread::operator=(thread&& other){
9698

@@ -104,9 +106,6 @@ namespace pthread {
104106
std::swap(_status, other._status);
105107
}
106108

107-
thread::~thread () {
108-
}
109-
110109
abstract_thread::abstract_thread(const std::size_t stack_size): _stack_size(stack_size){
111110
}
112111

tests/without-cpp11-pthread-tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ int main(int argc, const char * argv[]) {
111111
} catch ( ... ){
112112
message("Unhandled exception was thrown in main");
113113
}
114-
pthread::this_thread::sleep(10*1000); // sleep 10 seconds
114+
pthread::this_thread::sleep(5*1000); // sleep 10 seconds
115115
message( "end reached");
116116
}

0 commit comments

Comments
 (0)