Re: [PATCH v2 09/18] perf test: Add named_threads workload

From: Leo Yan

Date: Wed Jun 03 2026 - 11:26:02 EST


On Tue, Jun 02, 2026 at 03:26:51PM +0100, James Clark wrote:

> +#define DEFINE_THREAD(n) \
> +noinline void *named_threads_thread##n(void *arg __maybe_unused) \
> +{ \
> + pthread_setname_np(pthread_self(), "thread" #n); \
> + for (int i = 0; i < iterations; i++) \
> + named_threads_work *= 3; \
> + \
> + return NULL; \
> +}

> +static int named_threads(int argc, const char **argv)
> +{
> + pthread_t threads[MAX_THREADS];
> + int nr_threads = 1;
> + int err = 0;
> +
> + if (argc > 0)
> + nr_threads = atoi(argv[0]);
> +
> + if (nr_threads <= 0 || nr_threads > MAX_THREADS) {
> + fprintf(stderr, "Error: num threads must be 1 - %d\n", MAX_THREADS);
> + return 1;
> + }
> +
> + if (argc > 1)
> + iterations = atoi(argv[1]);
> +
> + if (iterations < 0) {
> + fprintf(stderr, "Error: iterations must be non-negative\n");
> + return 1;
> + }
> +
> + for (int i = 0; i < nr_threads; i++) {
> + int ret;
> +
> + ret = pthread_create(&threads[i], NULL, thread_fns[i], NULL);

Just curious this can be simplified to a thread function, like:

noinline void *named_thread(void *arg)
{
char name[16];

snprintf(name, sizeof(name), "thread%d", int(arg));

pthread_setname_np(pthread_self(), name);
...
return NULL;
}

Thanks,
Leo