{"action":"created","issue":{"url":"https://api.github.com/repos/libuv/libuv/issues/247","labels_url":"https://api.github.com/repos/libuv/libuv/issues/247/labels{/name}","comments_url":"https://api.github.com/repos/libuv/libuv/issues/247/comments","events_url":"https://api.github.com/repos/libuv/libuv/issues/247/events","html_url":"https://github.com/libuv/libuv/issues/247","id":60044753,"number":247,"title":"feature request: thread start/exit callbacks","user":{"login":"beevik","id":466481,"avatar_url":"https://avatars.githubusercontent.com/u/466481?v=3","gravatar_id":"","url":"https://api.github.com/users/beevik","html_url":"https://github.com/beevik","followers_url":"https://api.github.com/users/beevik/followers","following_url":"https://api.github.com/users/beevik/following{/other_user}","gists_url":"https://api.github.com/users/beevik/gists{/gist_id}","starred_url":"https://api.github.com/users/beevik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beevik/subscriptions","organizations_url":"https://api.github.com/users/beevik/orgs","repos_url":"https://api.github.com/users/beevik/repos","events_url":"https://api.github.com/users/beevik/events{/privacy}","received_events_url":"https://api.github.com/users/beevik/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-03-06T01:20:33Z","updated_at":"2015-03-06T22:21:59Z","closed_at":null,"body":"Some programs require (1) the ability to initialize per-thread resources whenever a new thread starts executing, and (2) the ability to clean up resources just before the thread exits. For example, performance monitoring tools relying on code instrumentation often initialize per-thread counters on thread start-up and flush them to an output device just before the thread exits.\r\n\r\nIn libuv there appear to be 3 ways threads can be created:\r\n 1. The user directly calls uv_thread_create.\r\n 2. The thread pool creates a number of worker threads in response to various libuv work scheduling requests (e.g., fs jobs, DNS address lookups).\r\n 3. On OSX, uv__stream_try_select can create a new thread.\r\n\r\nIn only the first case can the user control the initialization and clean-up of per-thread resources. In the remaining two cases, it's impossible because there is no direct call into the user's code when the thread starts or exits.\r\n\r\nTo remedy this, I'd like to propose two new libuv APIs:\r\n* One to set an optional callback to be called from within a new thread right after it begins executing.\r\n* One to set an optional callback to be called from within a thread right before it exits.\r\n\r\nI've created a branch with the proposed change. See https://github.com/beevik/libuv/releases/tag/thread-cb for the first draft.  If you think this is a change worth considering for inclusion in libuv, let me know and I'll make a pull request out of it.\r\n\r\nIt's possible I missed a way to do this within the current libuv API, so please let me know if that's the case."},"comment":{"url":"https://api.github.com/repos/libuv/libuv/issues/comments/77647184","html_url":"https://github.com/libuv/libuv/issues/247#issuecomment-77647184","issue_url":"https://api.github.com/repos/libuv/libuv/issues/247","id":77647184,"user":{"login":"beevik","id":466481,"avatar_url":"https://avatars.githubusercontent.com/u/466481?v=3","gravatar_id":"","url":"https://api.github.com/users/beevik","html_url":"https://github.com/beevik","followers_url":"https://api.github.com/users/beevik/followers","following_url":"https://api.github.com/users/beevik/following{/other_user}","gists_url":"https://api.github.com/users/beevik/gists{/gist_id}","starred_url":"https://api.github.com/users/beevik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beevik/subscriptions","organizations_url":"https://api.github.com/users/beevik/orgs","repos_url":"https://api.github.com/users/beevik/repos","events_url":"https://api.github.com/users/beevik/events{/privacy}","received_events_url":"https://api.github.com/users/beevik/received_events","type":"User","site_admin":false},"created_at":"2015-03-06T22:21:59Z","updated_at":"2015-03-06T22:21:59Z","body":"> I don't think you'd have to care about what it does though because it's completely internal to libuv.\r\n\r\nThat's probably true for most, but our use case entails overriding the memory allocator. So if the long-lived thread function calls uv__malloc and we are trying to associate those allocations with their threads, it might still be a problem for us."}}