0

我正在尝试在 C 中创建具有连续 id 号的线程。例如,假设我想创建 10 个线程,然后我想给它们 1 到 10 的 id。稍后,我希望能够访问这些 id 并从线程函数中打印出来。这可行吗?

我知道这可能看起来很简单,但我还没有设法在任何地方找到解决方案。

谢谢

4

1 回答 1

3

线程 ID 由操作系统或线程库创建。你无法控制它们会是什么。

您不需要 ID 是连续的。创建一个数组并将每个线程的 ID 存储在数组中。然后您可以使用数组按顺序访问它们。

像这样的东西(假设你使用 pthreads):

pthread_t thread_list[100];
int thread_count = 0;

...

pthread_create(&thread_list[thread_count++], NULL, thread_function, NULL);
于 2016-07-20T18:50:33.067 回答