Skip to content

Commit 16e4bf0

Browse files
committed
Bug fix on threads (patch provided by C.P.)
1 parent 7569c39 commit 16e4bf0

File tree

3 files changed

+57
-69
lines changed

3 files changed

+57
-69
lines changed

src/TkUtil/Threads.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Mutex ThreadIfc::_mutex;
4141

4242

4343
ThreadIfc::ThreadIfc (size_t concurrencyFlag)
44-
: _concurrencyFlag (concurrencyFlag), _thread (0),
45-
_running (false), _cancelled (false)
46-
{
44+
: _concurrencyFlag (concurrencyFlag), _thread (0), _running (true), _cancelled (false) // v 6.2.0
45+
{ // v 6.2.0 : true est affecté d'emblée à _running car il pouvait arriver que join soit invoqué avant que startTask
46+
// n'aie appelé run_thread ou que tout au moins que run_thread n'aie affecté true à running.
4747
registerThread (this);
4848
} // ThreadIfc::ThreadIfc
4949

@@ -104,8 +104,7 @@ ThreadIfc* ThreadIfc::withId (pthread_t id)
104104

105105
AutoMutex mutex (&_mutex);
106106

107-
for (vector<ThreadIfc*>::iterator it = _threads.begin ( );
108-
_threads.end ( ) != it; it++)
107+
for (vector<ThreadIfc*>::iterator it = _threads.begin ( ); _threads.end ( ) != it; it++)
109108
{
110109
CHECK_NULL_PTR_ERROR (*it)
111110
if ((*it)->getId ( ) == id)
@@ -132,8 +131,7 @@ void ThreadIfc::unregisterThread (ThreadIfc* thread)
132131
AutoMutex mutex (&_mutex);
133132

134133
CHECK_NULL_PTR_ERROR (thread)
135-
for (vector<ThreadIfc*>::iterator it = _threads.begin ( );
136-
_threads.end ( ) != it; it++)
134+
for (vector<ThreadIfc*>::iterator it = _threads.begin ( ); _threads.end ( ) != it; it++)
137135
{
138136
if (*it == thread)
139137
{
@@ -155,8 +153,7 @@ void* ThreadIfc::run_thread (void* t)
155153

156154
try
157155
{
158-
// ThreadManager::instance ( ) lève une exception si l'API n'est pas
159-
// initialisée.
156+
// ThreadManager::instance ( ) lève une exception si l'API n'est pas initialisée.
160157
ThreadManager::instance ( ).taskCompleted (thread);
161158
}
162159
catch (...)
@@ -250,8 +247,7 @@ void JoinableThread::startTask ( )
250247
#ifdef FORCE_THREADS_STACK_SIZE
251248
CHECK_POSIX_CALL (pthread_attr_setstacksize(&attr,(2*PTHREAD_STACK_MIN)), "pthread_attr_setstacksize")
252249
#endif // FORCE_THREADS_STACK_SIZE
253-
CHECK_POSIX_CALL (pthread_create (&getPosixThread ( ), &attr,
254-
ThreadIfc::run_thread, this), "pthread_create")
250+
CHECK_POSIX_CALL (pthread_create (&getPosixThread ( ), &attr, ThreadIfc::run_thread, this), "pthread_create")
255251
CHECK_POSIX_CALL (pthread_attr_destroy (&attr), "pthread_attr_destroy")
256252
} // JoinableThread::startTask
257253

@@ -261,8 +257,7 @@ void JoinableThread::join ( )
261257
if (true == isRunning ( ))
262258
{
263259
void* status = 0;
264-
CHECK_POSIX_CALL(
265-
pthread_join (getPosixThread ( ), &status), "pthread_join")
260+
CHECK_POSIX_CALL(pthread_join (getPosixThread ( ), &status), "pthread_join")
266261
} // if (true == isRunning ( ))
267262
} // JoinableThread::join
268263

src/tests/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ add_test(NAME exceptions COMMAND $<TARGET_FILE:exceptions>)
4141
add_test(NAME fileinfos COMMAND $<TARGET_FILE:fileinfos> /etc/os-release)
4242
add_test(NAME fileopts COMMAND $<TARGET_FILE:fileopts>)
4343
add_test(NAME hostinfos COMMAND $<TARGET_FILE:hostinfos>)
44-
# joinable often triggers SEGFAULT on github CI... don't understand why
45-
# add_test(NAME joinable COMMAND $<TARGET_FILE:joinable>)
44+
add_test(NAME joinable COMMAND $<TARGET_FILE:joinable>)
4645
add_test(NAME logs COMMAND $<TARGET_FILE:logs>)
4746
add_test(NAME memory COMMAND $<TARGET_FILE:memory>)
4847
execute_process (COMMAND bash -c "touch /tmp/my_empty_file")

src/tests/joinable.cpp

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -117,68 +117,62 @@ int main (int argc, char* argv[])
117117
cout << "THR " << (unsigned long)current << " PRIORITY : " << param.__sched_priority << " POLICY : " << policy << endl; */
118118
try
119119
{
120-
const size_t nbProcs = MachineData::instance ( ).getProcessorsNum ( );
121-
122-
cout << "Computer " << NetworkData::getCurrentHostName ( ) << " has "
123-
<< nbProcs << " processors." << endl;
124-
125-
vector <JoinableThread*> tasks;
126-
size_t i = 0;
127-
const size_t max = 10;
128-
Writer writer (stdout);
129-
cout << "Creation of " << (unsigned long)max
130-
<< " joinable writing tasks without mutex ..." << endl;
131-
for (i = 0; i < max; i++)
132-
tasks.push_back (new WriterThread (max, writer));
133-
cout << "Running tasks ..." << endl;
134-
for (i = 0; i < max; i++)
135-
tasks [i]->startTask ( );
136-
// Rem : sched_yield important, on peut sinon observer des plantages,
137-
// probablement à cause de tâches qui ne se sont pas encore lancées.
138-
sched_yield ( );
139-
cout << "Joining tasks ..." << endl;
140-
for (i = 0; i < max; i++)
141-
{
142-
tasks [i]->join ( );
143-
}
144-
cout << "Joinable writing tasks without mutex completed." << endl;
145-
cout << "Deleting tasks ..." << endl;
146-
for (i = 0; i < max; i++)
147-
delete tasks [i];
148-
cout << endl << endl;
149-
150-
tasks.clear ( );
151-
ProtectedWriter pwriter (stdout);
152-
cout << "Creation of " << (unsigned long)max
153-
<< " joinable writing tasks with mutexes ..." << endl;
154-
for (i = 0; i < max; i++)
155-
tasks.push_back (new WriterThread (max, pwriter));
156-
cout << "Running tasks ..." << endl;
157-
for (i = 0; i < max; i++)
158-
tasks [i]->startTask ( );
159-
sched_yield ( );
160-
cout << "Joining tasks ..." << endl;
161-
for (i = 0; i < max; i++)
162-
{
163-
tasks [i]->join ( );
164-
// delete tasks [i];
165-
cout << "Deleting tasks." << endl;
166-
}
167-
for (i = 0; i < max; i++)
168-
{
169-
delete tasks [i];
170-
}
171-
cout << "Joinable writing tasks with mutexes completed." << endl;
120+
121+
const size_t nbProcs = MachineData::instance ( ).getProcessorsNum ( );
122+
123+
cout << "Computer " << NetworkData::getCurrentHostName ( ) << " has " << nbProcs << " processors." << endl;
124+
125+
vector <JoinableThread*> tasks;
126+
size_t i = 0;
127+
const size_t max = 10;
128+
Writer writer (stdout);
129+
cout << "Creation of " << (unsigned long)max << " joinable writing tasks without mutex ..." << endl;
130+
for (i = 0; i < max; i++)
131+
tasks.push_back (new WriterThread (max, writer));
132+
cout << "Running tasks ..." << endl;
133+
for (i = 0; i < max; i++)
134+
tasks [i]->startTask ( );
135+
// Rem : sched_yield important, on peut sinon observer des plantages, probablement à cause de tâches qui ne se sont pas encore lancées.
136+
// sched_yield ( ); // v 6.2.0 : not still usefull
137+
cout << "Joining tasks ..." << endl;
138+
for (i = 0; i < max; i++)
139+
{
140+
tasks [i]->join ( );
141+
}
142+
cout << "Joinable writing tasks without mutex completed." << endl;
143+
cout << "Deleting tasks ..." << endl;
144+
for (i = 0; i < max; i++)
145+
delete tasks [i];
146+
cout << endl << endl;
147+
148+
tasks.clear ( );
149+
ProtectedWriter pwriter (stdout);
150+
cout << "Creation of " << (unsigned long)max << " joinable writing tasks with mutexes ..." << endl;
151+
for (i = 0; i < max; i++)
152+
tasks.push_back (new WriterThread (max, pwriter));
153+
cout << "Running tasks ..." << endl;
154+
for (i = 0; i < max; i++)
155+
tasks [i]->startTask ( );
156+
// sched_yield ( ); // v 6.2.0 : not still usefull
157+
cout << "Joining tasks ..." << endl;
158+
for (i = 0; i < max; i++)
159+
{
160+
tasks [i]->join ( );
161+
// delete tasks [i];
162+
cout << "Deleting tasks." << endl;
163+
}
164+
for (i = 0; i < max; i++)
165+
delete tasks [i];
166+
cout << "Joinable writing tasks with mutexes completed." << endl;
167+
172168
}
173169
catch (const Exception& exc)
174170
{
175171
cout << "Exception caught : " << exc.getFullMessage ( ) << endl;
176-
return -1;
177172
}
178173
catch (...)
179174
{
180175
cout << "Unexpected error caught." << endl;
181-
return -1;
182176
}
183177

184178
return 0;

0 commit comments

Comments
 (0)