【问题标题】:PThread Mutex Lock & Unlock on Stack堆栈上的 PThread 互斥锁和解锁
【发布时间】:2013-01-28 15:39:12
【问题描述】:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS     2

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

char **AddToStack(char *buffer)
{
  static char **stack = NULL;
  int stacksize = 0;

  if (buffer == NULL)
    return stack;

  if (stack == NULL){
    stack = calloc(1, sizeof(char*));
  }
  stack[stacksize] = strdup(buffer);
  stacksize++;
  stack = realloc(stack, (stacksize+1) * sizeof(char*));
  stack[stacksize] = NULL;
  return stack;
}

void *FRead(void *threadid)
{
pthread_mutex_lock(&mutex);
  char fname[256], buffer[256];
  FILE *ifile;
  long tid;
  int counter;
  tid = (long)threadid;

  sprintf(fname, "data%ld", tid);
  if ((ifile = fopen(fname, "r")) == NULL){
    printf("Error: Thread #%ld was unable to open file %s!\n", tid, fname);
  }
  else{
    printf("Thread #%ld starting to read from file %s!\n", tid, fname);
    fscanf(ifile, "%s", buffer);
    counter = 0;
 while (!feof(ifile)){
//pthread_mutex_lock(&mutex);  
    counter++;
      AddToStack(buffer);
      fscanf(ifile, "%s", buffer);
//pthread_mutex_unlock(&mutex);
    }
    fclose(ifile);
    printf("Thread #%ld added %d entries to the stack!\n", tid, counter);
pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
  }
  return NULL;
}

int main(int argc, char *argv[])
{
  pthread_t threads[NUM_THREADS];
  int rc, i = 0;
  long t;
  char **stack;
  FILE *ofile;

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


for(t=0;t<NUM_THREADS;t++)
{
//pthread_join(threads[t],NULL);
}


  //Lets write the content of the stack into a file
  stack = AddToStack(NULL); //Get the base of the stack pointer
  if (stack != NULL){
    ofile = fopen("result.dat", "w");
    for (i = 0; stack[i] != NULL; i++)
      fprintf(ofile, "%s\n", stack[i]);
    fclose(ofile);
  }
  fprintf(stderr, "%d data written to file result.dat\n", i);
  pthread_exit(NULL);
  return 0;
}

这是我的 data1 和 data2 文件

数据1: 放弃 贬低 贬低 降级 惑 羞愧 羞愧 羞辱 贬低 减弱 减弱 减排 减弱 减少 屠宰场 修道院 中止 缩写 缩写 缩写 缩写 缩写 缩写 放弃 退位 退位 退位 腹部 拐 被绑架了

数据2:

God
I
I'll
I'm
I've
Miss
Worry
a
ability
able
aboard
about
above
abroad
absence
absent
absolute
accident
accidentally
according
accordingly
account
acquaintance
across
act
action
active
activity
actual
admiration
admission
adress
advance
advantage
adventure

上面是我的互斥代码,但我想知道为什么我只能将 1 个数据写入文件 result.dat

这是我的输出

1个数据写入文件result.dat

【问题讨论】:

    标签: c++ c


    【解决方案1】:

    首先,pthread_mutex_unlock(&amp;mutex);pthread_exit(NULL); 位置不正确。将其放在return NULL 语句之前。

    代码中有几个错误。请在下面找到更新的代码。

        #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define NUM_THREADS     2
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    char **AddToStack(char *buffer)
    {
      static char **stack = NULL;
      static int stacksize = 0;
    
      if (buffer == NULL)
        return stack;
    
      if (stack == NULL){
        stack = (char**)calloc(1, sizeof(char*));
      }
      stack[stacksize] = strdup(buffer);
      stacksize++;
      stack = (char**)realloc(stack, (stacksize+1) * sizeof(char*));
      stack[stacksize] = NULL;
      return stack;
    }
    
    void *FRead(void *threadid)
    {
      pthread_mutex_lock(&mutex);
      char fname[256], buffer[256];
      FILE *ifile;
      long tid;
      int counter;
      tid = (long)threadid;
    
      sprintf(fname, "data%ld", tid);
      if ((ifile = fopen(fname, "r")) == NULL){
        printf("Error: Thread #%ld was unable to open file %s!\n", tid, fname);
      }
      else{
        printf("Thread #%ld starting to read from file %s!\n", tid, fname);
        fscanf(ifile, "%s", buffer);
        counter = 0;
        while (!feof(ifile)){
          //pthread_mutex_lock(&mutex);  
          counter++;
          AddToStack(buffer);
          fscanf(ifile, "%s", buffer);
          //pthread_mutex_unlock(&mutex);
        }
        fclose(ifile);
        printf("Thread #%ld added %d entries to the stack!\n", tid, counter);
        //pthread_mutex_unlock(&mutex);
        //pthread_exit(NULL);
      }
      pthread_mutex_unlock(&mutex);
      pthread_exit(NULL);
      return NULL;
    }
    
    int main(int argc, char *argv[])
    {
      pthread_t threads[NUM_THREADS];
      int rc, i = 0;
      long t;
      char **stack;
      FILE *ofile;
    
      for(t=0;t<NUM_THREADS;t++){
        printf("In main: creating thread %ld\n", t);
        rc = pthread_create(&threads[t], NULL, FRead, (void *)t);
        if (rc){
          printf("ERROR; return code from pthread_create() is %d\n", rc);
          exit(-1);
        }
      }
    
    
      for(t=0;t<NUM_THREADS;t++)
      {
        //pthread_join(threads[t],NULL);
      }
    
    
      //Lets write the content of the stack into a file
      stack = AddToStack(NULL); //Get the base of the stack pointer
      if (stack != NULL){
        ofile = fopen("result.dat", "w");
        for (i = 0; stack[i] != NULL; i++)
          fprintf(ofile, "%s\n", stack[i]);
        fclose(ofile);
      }
      fprintf(stderr, "%d data written to file result.dat\n", i);
      pthread_exit(NULL);
      return 0;
    }
    

    【讨论】:

    • 我试过你的答案,但它不起作用。 0 数据写入文件 result.dat
    • 那是因为 stacksize 是局部变量。这意味着所有值将始终在第零个索引上被覆盖。我已经更改了代码。
    猜你喜欢
    • 1970-01-01
    • 2013-06-06
    • 2018-05-23
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    相关资源
    最近更新 更多