Skip to content

Commit f6c2956

Browse files
committed
feat(ipc/server): make runner thread detached
The socket thread will be used in pthread_join, but the run thread must be detached, and will not be joined. Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent b90941c commit f6c2956

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/samples/supervisor/posix/server/runners.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ static void *socket_thread(void *arg)
129129
int container_runner_dispatch(struct ocre_container *container, int socket)
130130
{
131131
struct runner *runner = NULL;
132+
pthread_attr_t attr;
132133

133134
runner = malloc(sizeof(struct runner));
134135
if (!runner) {
@@ -147,18 +148,37 @@ int container_runner_dispatch(struct ocre_container *container, int socket)
147148
goto error_runner;
148149
}
149150

151+
rc = pthread_attr_init(&attr);
152+
if (rc) {
153+
fprintf(stderr, "Failed to initialize thread attributes: rc=%d", rc);
154+
goto error_mutex;
155+
}
156+
157+
rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
158+
if (rc) {
159+
fprintf(stderr, "Failed to initialize thread attributes: rc=%d", rc);
160+
goto error_attr;
161+
}
162+
163+
/* The socket thread is joinable */
150164
rc = pthread_create(&runner->socket_thread, NULL, socket_thread, runner);
151165
if (rc) {
152166
fprintf(stderr, "Failed to create socket thread: rc=%d", rc);
153167
goto error_mutex;
154168
}
155169

156-
rc = pthread_create(&runner->run_thread, NULL, run_thread, runner);
170+
/* The run_thread is detached */
171+
rc = pthread_create(&runner->run_thread, &attr, run_thread, runner);
157172
if (rc) {
158173
fprintf(stderr, "Failed to create runner thread: rc=%d", rc);
159174
goto error_thread;
160175
}
161176

177+
rc = pthread_attr_destroy(&attr);
178+
if (rc) {
179+
fprintf(stderr, "Failed to destroy thread attributes: rc=%d", rc);
180+
}
181+
162182
return 0;
163183

164184
error_thread:
@@ -176,6 +196,12 @@ int container_runner_dispatch(struct ocre_container *container, int socket)
176196

177197
pthread_join(runner->socket_thread, NULL);
178198

199+
error_attr:
200+
rc = pthread_attr_destroy(&attr);
201+
if (rc) {
202+
fprintf(stderr, "Failed to destroy thread attributes: rc=%d", rc);
203+
}
204+
179205
error_mutex:
180206
pthread_mutex_destroy(&runner->mutex);
181207

0 commit comments

Comments
 (0)