在 'paralelism' 中执行线程

Executing threads in 'paralelism'

我有一个数字范围(即 1~10000)。
我需要创建 threads 来搜索值 X。
每个线程都会有自己的时间间隔来搜索它(即 10000/threadNumber)。
我想把线程 运行 按顺序排列是没有意义的。我无法让它们同时 运行...

到目前为止我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define limit 10000
#define n_threads 2


void* func1(void* arg)
{
    int i=0, *value = (int *)arg;
//How may I know which thread is running and make the thread search for the right range of values ?
    for(i=1; i<=limit/n_threads; i++)
    {
        if(*value == i){
           //Print the thread ID and the value found.
        }
        else
          //Print the thread ID and the value 0.
    }
    return NULL;
}

int main(int argc, char const *argv[])
{
    if(argc < 2)
        printf("Please, informe the value you want to search...\n");
    else{
        pthread_t t1, t2;
        int x = atoi(argv[1]);  //Value to search

        pthread_create(&t1, NULL, func1, (void *)(&x));
        pthread_create(&t2, NULL, func1, (void *)(&x));
        pthread_join(t1, NULL);
        pthread_join(t2, NULL);
    }

    return 0;
}  

目前遇到的问题:

获取线程 ID 因操作系统而异。

参见how to get thread id of a pthread in linux c program? as @user3078414 mentioned, and why compiler says ‘pthread_getthreadid_np’ was not declared in this scope?

感谢@Dmitri,这是一个将多个值传递给线程函数的示例。线程 运行 并发。 Mutexes 是另外一章,专门讨论共享数据以及如何访问它。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define limit 10000
#define n_threads 2

struct targs {
  int from;
  int to;
};

void *func1(void *arg) {
  struct targs *args = (struct targs *) arg;
  printf("%d => %d\n", args->from, args->to);
  // free(arg)
  return NULL;
}

int main(int argc, char const *argv[]) {
  struct targs *args;
  pthread_t t1;
  pthread_t t2;

  args = (struct targs *) malloc(sizeof(args));
  args->from = 0;
  args->to = 100;
  pthread_create(&t1, NULL, func1, (void *) args);

  args = (struct targs *) malloc(sizeof(args));
  args->from = 100;
  args->to = 200;
  pthread_create(&t2, NULL, func1, (void *) args);

  pthread_join(t1, NULL);
  pthread_join(t2, NULL);

  return 0;
}