简体   繁体   English

c多线程编程中的pthread_create参数

[英]pthread_create argument in c multi thread programming

pthread_create(&Thread,NULL,ChildThread,(void *)100); pthread_create(&Thread,NULL,ChildThread,(void *)100);

1) Can we pass the 4th argument of pthread_create as shown above? 1)我们可以传递pthread_create的第4个参数,如上所示? shouldn't it be a pointer variable? 它不应该是指针变量吗?

Just an example ( not meant to be correct way of doing it; but to serve as example code for anyone who want to play with it ): 只是一个例子( not meant to be correct way of doing it; but to serve as example code for anyone who want to play with it ):

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>

void *print_number(void *number) {
    printf("Thread received parameter with value: %d\n", number);
    return (void *)number;
}

int main(int argc, char *argv[]) {
    pthread_t thread;
    void *ret;
    int pt_stat;
    pt_stat = pthread_create(&thread, NULL, print_number, (void *)100);
    if (pt_stat) {
        printf("Error creating thread\n");
        exit(0);
    }

    pthread_join(thread, &ret);
    printf("Return value: %d\n", ret);

    pthread_exit(NULL);

    return 0;
}

This will lead to undefined behavior if the pointer value is greater then what an int can hold. 如果指针值大于int可以容纳的值,这将导致未定义的行为。 See this quote from C99: 从C99看到这个引用:

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

What (void *)100 means is take the integer value 100 and treat it as a pointer to some unspecified type of memory (ie, (void *) ). What (void *)100表示取整数值100并将其视为指向某些未指定类型的内存的指针(即(void *) )。 In that case, that means push the integer value 100 on the stack as an argument to pthread_create . 在这种情况下,这意味着将堆栈上的整数值100作为pthread_create的参数。 Presumably, ChildThread casts the void * passed to it back to an int , then uses it as a number. 据推测, ChildThread将传递给它的void *转换回int ,然后将其用作数字。

Fundamentally pointers are really just memory addresses. 从根本上说,指针实际上只是内存地址。 A memory address is just a number that describes a location in memory, so casting an int to a pointer of any type is legal. 内存地址只是一个描述内存中位置的数字,因此将int转换为任何类型的指针都是合法的。 There are a few cases where casting an int to a pointer is absolutely the right thing to do and required, however, they tend to be rare. 在某些情况下,将int转换为指针绝对是正确的做法和要求,但是,它们往往很少见。 For example, if you are writing code for an embedded controller, and want write a driver for a memory mapped I/O device, then you might cast the device's base address as a pointer to an int or struct and then do normal C accesses through the pointer to access the device. 例如,如果您正在为嵌入式控制器编写代码,并且想要为内存映射I / O设备编写驱动程序,那么您可以将设备的基址转换为指向int或struct的指针,然后通过正常的C访问来执行访问设备的指针。 Another example where casting int s to pointers, would be to implement the low-level virtual memory management routines to parcel out physical memory for an operating system. int转换为指针的另一个例子是实现低级虚拟内存管理例程,以便为操作系统分配物理内存。

The code you present is not uncommon and will work, assuming that the size of a pointer is at least big enough to hold the integer you are trying to pass. 您提供的代码并不常见,并且可以正常工作,假设指针的大小至少足以容纳您尝试传递的整数。 Most systems that implement pthread_create would probably have a 32-bit or 64-bit pointer, so your example is pretty likely to work. 大多数实现pthread_create系统可能都有32位或64位指针,因此您的示例很可能会起作用。 IMHO, it is a bit of an abuse, because 100 probably does not refer to a memory location in this case, and C does not guarantee that a void * is big enough to hold an int . 恕我直言,这有点滥用,因为在这种情况下100可能不会引用内存位置,并且C不保证void *足以容纳int

Taken from an excellent artice on POSIX Thread Progreamming . 取自POSIX线程Progreamming的优秀artice Must read for any newbie . 必须阅读任何新手。

Example Code - Pthread Creation and Termination

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}

Explanation : 说明:

You can pass the 100 as the 4th argument to the pthread_create() . 您可以将100作为第4个参数传递给pthread_create() In the function PrintHello you can typecast the void* back into the correct type . 在PrintHello函数中,您可以将void *转换回正确的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM