将参数传递给pthread_create函数

时间:2021-04-25 16:02:22

I use pthread_create to create 10 child threads, passes an integer to the thread_func

我使用pthread_create创建10个子线程,将一个整数传递给thread_func

#define THREAD_NUM 10

void *thread_func(void *arg)
{
    int v = (int)arg;

    printf("v = %d\n", v);

    return (void*)0;
}

int main(int argc, const char *argv[])
{
    pthread_t pids[THREAD_NUM];
    int rv;
    int i;

    for (i = 0; i < THREAD_NUM; i++) {
        rv = pthread_create(&pids[i], NULL, thread_func, (void*)i);
        if (rv != 0) {
           perror("failed to create child thread");
           return 1;
        }
    }
    return 0;
}

I was wondering why it outputs different result everytime not just v = 1 v = 2 ... v = 9

我想知道为什么每次输出不同的结果而不仅仅是v = 1 v = 2 ... v = 9

4 个解决方案

#1


1  

You have to wait for all the threads to complete in the main using pthread_join, only then u can see all of them display some value

你必须等待所有线程在主要使用pthread_join完成,然后你才能看到所有线程都显示一些值

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

#define THREAD_NUM 10

void *thread_func(void *arg)
{
    int v = (int)arg;

    printf("v = %d\n", v);

    return (void*)0;
}

int main(int argc, const char *argv[])
{
    pthread_t pids[THREAD_NUM];
    int rv;
    int i;

    for (i = 0; i < THREAD_NUM; i++) {
        rv = pthread_create(&pids[i], NULL, thread_func, (void*)i);
        if (rv != 0) {
           perror("failed to create child thread");
           return 1;
        }
    }
    for (i = 0; i < THREAD_NUM; i++) {
        pthread_join(pids[i], NULL);
    }
    return 0;
}

Sample run output:

样本运行输出:

[root@fc ~]# ./a.out
v = 0
v = 2
v = 4
v = 6
v = 7
v = 8
v = 9
v = 5
v = 3
v = 1

#2


0  

I think your question is why it is not printing them in order like v=1 v = 2 ... v = 9. That is because the kernel schedules the threads and they can be scheduled in any order. If you want synchronized output you need to use locks and condition variables.

我认为你的问题是它不按照v = 1 v = 2 ... v = 9的顺序打印它们。这是因为内核调度线程并且可以按任何顺序进行调度。如果需要同步输出,则需要使用锁和条件变量。

#3


0  

In my case I am having proper out put. I have done some modification. Can you give a try to it?

在我的情况下,我有适当的出局。我做了一些修改。你能尝试一下吗?

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

#define THREAD_NUM 10

void *thread_func(void *arg)
{
  int v = *(int *)arg;

  printf("v = %d\n", v);

  return (void*)0;
}

int main(int argc, const char *argv[])
{
  pthread_t pids[THREAD_NUM];
  int rv;
  int i;

  for (i = 0; i < THREAD_NUM; i++) {
    rv = pthread_create(&pids[i], NULL, thread_func, (void*)&i);
    if (rv != 0) {
      perror("failed to create child thread");
      return 1;
    }
  }
  return 0;
}

#4


0  

Briefly, echo of the threads in your program is an independent scheduling unit, which means they can actually or virtually run parallelly in an order determined by the scheduler in your operating system kernel, and this order changes run to run according to your system circumstances at that time.

简而言之,程序中线程的回显是​​一个独立的调度单元,这意味着它们可以按照操作系统内核中的调度程序确定的顺序实际或虚拟地并行运行,并且此顺序更改运行根据您的系统环境运行那时。

#1


1  

You have to wait for all the threads to complete in the main using pthread_join, only then u can see all of them display some value

你必须等待所有线程在主要使用pthread_join完成,然后你才能看到所有线程都显示一些值

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

#define THREAD_NUM 10

void *thread_func(void *arg)
{
    int v = (int)arg;

    printf("v = %d\n", v);

    return (void*)0;
}

int main(int argc, const char *argv[])
{
    pthread_t pids[THREAD_NUM];
    int rv;
    int i;

    for (i = 0; i < THREAD_NUM; i++) {
        rv = pthread_create(&pids[i], NULL, thread_func, (void*)i);
        if (rv != 0) {
           perror("failed to create child thread");
           return 1;
        }
    }
    for (i = 0; i < THREAD_NUM; i++) {
        pthread_join(pids[i], NULL);
    }
    return 0;
}

Sample run output:

样本运行输出:

[root@fc ~]# ./a.out
v = 0
v = 2
v = 4
v = 6
v = 7
v = 8
v = 9
v = 5
v = 3
v = 1

#2


0  

I think your question is why it is not printing them in order like v=1 v = 2 ... v = 9. That is because the kernel schedules the threads and they can be scheduled in any order. If you want synchronized output you need to use locks and condition variables.

我认为你的问题是它不按照v = 1 v = 2 ... v = 9的顺序打印它们。这是因为内核调度线程并且可以按任何顺序进行调度。如果需要同步输出,则需要使用锁和条件变量。

#3


0  

In my case I am having proper out put. I have done some modification. Can you give a try to it?

在我的情况下,我有适当的出局。我做了一些修改。你能尝试一下吗?

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

#define THREAD_NUM 10

void *thread_func(void *arg)
{
  int v = *(int *)arg;

  printf("v = %d\n", v);

  return (void*)0;
}

int main(int argc, const char *argv[])
{
  pthread_t pids[THREAD_NUM];
  int rv;
  int i;

  for (i = 0; i < THREAD_NUM; i++) {
    rv = pthread_create(&pids[i], NULL, thread_func, (void*)&i);
    if (rv != 0) {
      perror("failed to create child thread");
      return 1;
    }
  }
  return 0;
}

#4


0  

Briefly, echo of the threads in your program is an independent scheduling unit, which means they can actually or virtually run parallelly in an order determined by the scheduler in your operating system kernel, and this order changes run to run according to your system circumstances at that time.

简而言之,程序中线程的回显是​​一个独立的调度单元,这意味着它们可以按照操作系统内核中的调度程序确定的顺序实际或虚拟地并行运行,并且此顺序更改运行根据您的系统环境运行那时。