【问题标题】:pthread same ID and output self_tpthread 相同 ID 并输出 self_t
【发布时间】:2015-01-10 19:06:46
【问题描述】:

我希望我能把我的问题说得很清楚,我正在编程 pthread,简单地说,我计算所需的线程数,并将创建的线程传递给函数并返回,函数确实在不同的块上转置;所以每个线程都有自己的块。

为了检查我是否发送了不同的线程,我运行 pthread_t self_t,但面临两个问题: 这似乎只使用了一个相同的线程,并且我总是有关于 selt_t 类型输出的警告消息,下面的代码简化显示主要品脱。 有什么想法我哪里出错了吗?

这里首先是struct和main:

pthread_mutex_t mutexZ;       // Mutex initialize
int array[nn][nn];

struct v
{
  int i, j; // threaded Row,Col
  int n, y; // 
  int iMAX; // 
};

void *transposeM(void *arg);

int main(int argc, char *argv[])
{

  int Thread_Num = 10; 
  pthread_t t_ID[Thread_Num]; // the number of threads depending on # blocks

  printf("Thread_Num %d\n", Thread_Num);

  struct v *data = (struct v *) malloc(sizeof(struct v));

  int i, j; //loop varables

  //#############################################################
  printf("Matrix Initial before Transpose Done\n"); 
  // printing the Matrix Before any transpose if needed testing

 for (i = 0; i < nn; i++){
  for(j = 0; j< nn; j++){
    array[i][j] = i*nn + j;
    printf("%d ", array[i][j]); 
   }
   printf("\n");}


  //************************************************************/
  // Initialize the mutex 
  pthread_mutex_init(&mutexZ, NULL);
  pthread_attr_t attr; //Set of thread attributes
  pthread_attr_init(&attr);

  int n, y; // Loop Variables for tiling 

  //************************************************************/ 
  //Start of loop transpose:

  int start = 0;

  for (n = 0; n < nn; n += TILE) 
  {
    data->n = n; // row

    for (y = 0; y <= n; y += TILE) {
      data->y = y; // column

      printf("y Tile:%d  \n", y);
      printf("Start before:%d  \n", start);

      //Transpose the other blocks, thread created for each Block transposed

      pthread_create(&(t_ID[start]), NULL, transposeM, (void*) data); // Send the thread to the function
      pthread_join(t_ID[start], NULL);

      if (start < Thread_Num)
      {
        start = start + 1;

      }
      printf("Start after:%d  \n", start);

    } // End the Y column TileJump loop
  } // End of n Row TileJump loop
} 

根据注释修改,

void *transposeM(void *arg)
{
  // Transposing the tiles
  struct v *data = arg;
  int i, j; //loop row and column
  int temp = 0;
  pthread_mutex_lock(&mutexZ); //lock the running thread here,so keeps block until thread that holds     mutex releases it

  pthread_t self_t; // To check the thread id - my check not Mandetory to use
  self_t = pthread_self();
  printf("Thread number Main = %u \n ", self_t); //here we used u% coz seems the pthread_t is unsigned long data type

  //*******************************************************
  //here some function to work
  //########################################################

  pthread_mutex_unlock(&mutexZ);
  pthread_exit(NULL);

  return (NULL);

} // End

【问题讨论】:

  • "pthread_t self_t;":不要为变量使用后缀_t,因为按照惯例,它表示一种类型。
  • return (&amp;data); 返回栈上的地址,线程结束后不再有效。然而,该语句将永远不会到达,因为在它之前已经调用了 pthread_exit(),它本身永远不会返回。

标签: c pthreads


【解决方案1】:

您的代码存在两个概念性问题:

  1. 您将相同的引用/地址传递给每个线程,使每个线程处理相同的数据。
  2. 创建线程后立即加入线程。作为连接块,直到要连接的线程结束,这使所有线程的运行顺序化。

为了解决 1. 为每个线程创建 data 指向的唯一实例。

修复 2. 将对 pthread_join() 的调用移出创建线程的循环,并将其置于创建循环后的第二个循环中。

...

printf("Thread_Num %d\n", Thread_Num);

pthread_t t_ID[Thread_Num]; // the number of threads depending on # blocks
struct v data_ID[Thread_Num] = {0}; // define an instance of data for ech thread

...

for (n = 0; n < nn; n += TILE) //limit of row
{
  struct v * data = data_ID + start; // assign thread specific instance

  data->n = n; // row

  for (y = 0; y <= n; y += TILE) // limit of column -here removd the =n, then diagonal tile is not     transposed
  {
    ...
    pthread_create(&(t_ID[start]), NULL, transposeM, (void*) data); // Send the thread to the function
    ...
  }
} // End the Y column TileJump loop

for (;start >= 0; --start)
{
  pthread_join(t_ID[start], NULL);
}

...

对线程函数的修改:

void *transposeM(void *arg)
{
  struct v *data = arg;

  ...

  pthread_t self = pthread_self(); // better naming

  ...

  pthread_exit(NULL); // the thread functions exits here.

  return NULL; // this is never reached, but is necessary to calm down thr compiler.
} // End

【讨论】:

  • 谢谢,首先您能否详细说明如何为数据创建唯一实例? 2. 我用什么替换“return(&data)”? 3.我添加了外部连接循环,现在它改变了线程号的数量,但是现在它没有在每个循环中进入函数,..它通过循环进入,但是即使我放了3次也进入函数线程数如 50,任何想法为什么?还有我用什么代替“Self_t”来显示线程数?
  • 请在我更新的答案中查看代码 sn-ps。 @user3673183
  • 您好,感谢您的笔记,首先:我的函数进入循环并且甚至没有实现 pthread_create,就像它跳过它一样,因为(这里应该进入大约 6 个循环,它的从最后三点开始输入。我将上面的代码修改为准备运行的最后一个注释。第二,输出pthread_t self打印它的格式是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
相关资源
最近更新 更多