c - POSIX Pthread 两个数字相加 C 代码

标签 c multithreading pthreads posix

我尝试使用下面的代码将指针传递给包含要添加的两个数字的数组

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

void *print_message_function( void *ptr );  

 main()  
 {  
      pthread_t thread1, thread2;  

      /*const char *message1 = "Thread 1";  

      const char *message2 = "Thread 2"; */

      int arr[2] = {5,8};
      const int *ptrtoarr;
      ptrtoarr=arr;

      int  iret1, iret2;  

     int *s=(int *)(ptrtoarr);
      printf("%d \n", *s); 
      ptrtoarr++;
      s=(int *)(ptrtoarr);
      printf("%d \n", *s); 
     ptrtoarr--;

     /* Create independent threads each of which will execute function */

    iret1 = pthread_create( &thread1, NULL, print_message_function,&arr);  

      if(iret1)  
      {  

          fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);  
          exit(EXIT_FAILURE);  
      }

    /*  iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);  

      if(iret2)      
      {  
          fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);  
          exit(EXIT_FAILURE);  
      }  */

      printf("pthread_create() for thread 1 returns: %d\n",iret1);  

     /* printf("pthread_create() for thread 2 returns: %d\n",iret2); */

      /* Wait till threads are complete before main continues. Unless we  */ 
      /* wait we run the risk of executing an exit which will terminate   */ 

      /* the process and all threads before the threads have completed.   */      
      pthread_join( thread1, NULL);  

      /* pthread_join( thread2, NULL); */  

      exit(EXIT_SUCCESS);  
 }  


 void *print_message_function( void *ptr )  
 {  
      /*char *message;  

      message = (char *) ptr;  

      printf("%s \n", message);  */
      printf("\n In message function \n");
      int *sum=(int *)(ptr);
      printf("%d \n", *sum); 
      sum = (int *)(ptr+1);
      printf("%d \n", *sum);


 } 

但是在包含附加步骤的函数 print_message_function 中,使用指针我可以访问第一个数字,但不能访问第二个数字。

(它在主函数中访问这两个数字)

输出如下

5 8 线程 1 的 pthread_create() 返回:0

在消息功能中 5 134217728

最佳答案

这里,

sum = (int *)(ptr+1);

您正在对 void *(void 指针)执行指针算术,这在标准 C 中是不允许的。GCC 允许对 void * 进行指针算术,方法是将其大小视为1.

应该是

  sum = (int *)ptr+1;

更简洁的方法是使用 sum 本身来获取下一个元素,而不是 ptr:

void *print_message_function( void *ptr )
{
  printf("\n In message function \n");
  int *sum=ptr;
  printf("%d \n", *sum); 
  sum++;
  printf("%d \n", *sum);
  return NULL;
} 

我删除了强制转换,因为 void * 与任何数据指针兼容。因此,强制转换是不必要的,并添加了 return NULL; 作为线程函数应返回 void * 或调用 pthead_exit()

GCC 有一个选项 -Wpointer-arith 来警告 void 指针上的指针算术。

关于c - POSIX Pthread 两个数字相加 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32290062/

相关文章:

c - 从二进制文件读取数据到C中的链表(访问违规读取位置)

java - 如何在java中指定时间延迟后启动线程

c# - 对我的应用程序中的线程安全性有何建议?

c - QEMU 用户模式模拟退出时是否会阻止 pthread_join 阻塞?

coding-style - 编码风格 : lock/unlock internal or external?

java - JNA结构创建带来麻烦

c - 从不同的 UART 驱动读取数据

更改依赖于变量的函数调用

python - 如何在Python中让线程等待?

c - pthread 的问题