Last active
August 26, 2022 04:06
-
-
Save basuke/e48193d908f03c5658270a2cfee0a279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the result on our platform (FreeBSD).