Skip to content

Instantly share code, notes, and snippets.

@basuke
Last active August 26, 2022 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basuke/e48193d908f03c5658270a2cfee0a279 to your computer and use it in GitHub Desktop.
Save basuke/e48193d908f03c5658270a2cfee0a279 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2022 Sony Interactive Entertainment Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <array>
pthread_key_t key;
__thread long param = 0;
static void* threadMain(void* arg)
{
param = reinterpret_cast<long>(arg);
pthread_setspecific(key, reinterpret_cast<void*>(0x1000 + param));
struct timespec remaining, request = { 0, param * 10'000'000};
nanosleep(&request, &remaining);
printf("Hello from thread #%ld.\n", param);
return NULL;
}
static void destructor(void* arg)
{
auto value = reinterpret_cast<long>(arg);
printf("Bye from destructor #%ld with value (0x%lx).\n", param, value);
param = 42;
pthread_setspecific(key, reinterpret_cast<void*>(param * 0x10000l));
}
int main(int, char* [])
{
constexpr int threadCount = 10;
std::array<pthread_t, threadCount> ids;
printf("Hello world from main.\n");
pthread_key_create(&key, destructor);
long no = 1;
for (auto& id : ids) {
pthread_create(&id, NULL, threadMain, reinterpret_cast<void*>(no++));
}
for (const auto& id : ids) {
pthread_join(id, NULL);
}
printf("Hello after pthread_join\n");
return 0;
}
@basuke
Copy link
Author

basuke commented Aug 26, 2022

Here's the result on our platform (FreeBSD).

Hello world from main.
Hello from thread #1.
Bye from destructor #1 with value (0x1001).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803d67e0 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #2.
Bye from destructor #2 with value (0x1002).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803d6c00 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #3.
Bye from destructor #3 with value (0x1003).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803d80aThe thread 0x18bdd has exited with code 0 (0x0).
The thread 0x18bde has exited with code 0 (0x0).
The thread 0x18bdf has exited with code 0 (0x0).
0 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #4.
Bye from destructor #4 with value (0x1004).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803d9540 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #5.
Bye from destructor #5 with value (0x1005).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803da9e0 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #6.
Bye from destructor #6 with value (0x1006).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803dbe80 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #7.
Bye from destructor #7 with value (0x1007).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803dd320 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #8.
Bye from destructor #8 with value (0x1008).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803de7c0 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #9.
Bye from destructor #9 with value (0x1009).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803dfc60 has exited with leftover thread-specific data after 4 destructor iterations
Hello from thread #10.
Bye from destructor #10 with value (0x100a).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Bye from destructor #42 with value (0x2a0000).
Thread 8803e1100 has exited with leftover thread-specific data after 4 destructor iterations
Hello after pthread_join
The thread 0x18be4 has exited with code 0 (0x0).
The thread 0x18be5 has exited with code 0 (0x0).
The thread 0x18be7 has exited with code 0 (0x0).
The thread 0x18be8 has exited with code 0 (0x0).
The thread 0x18bfa has exited with code 0 (0x0).
The thread 0x18c5c has exited with code 0 (0x0).
The thread 0x18d79 has exited with code 0 (0x0).
The thread 'sampleapp' (0x18b0f) has exited with code 0 (0x0).
The program '[118] sampleapp.self: CPU' has exited with code 0 (0x0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment