多线程同步

阿迪尔·阿拉姆

我如何同步这 9 个线程,以便它们在主线程之前执行?

我想检查大小为 9 的二维数组中行的有效性。每行应包含值(1 到 9)。为此,我在主线程中创建了一个名为“void* checksRows(void* arg)”的线程,并将其与 main 连接。然后线程checkingRows 又创建了9 个线程来检查每一行的有效性。

````````````````````````````````````
Pthread_t id1;
pthread_mutex_t mut1;
int arr[9][9] = {  
                    {6,2,4,5,3,9,1,8,7},
                    {6,6,9,7,2,8,6,3,4},
                    {8,3,7,6,1,4,2,9,5},
                    {1,4,3,8,6,5,7,2,9},
                    {9,5,8,2,4,7,3,6,1},
                    {7,6,2,3,9,1,4,5,8},
                    {3,7,1,9,5,6,8,4,2},
                    {4,9,6,1,8,2,5,7,3},
                    {2,8,5,4,7,3,9,1,6}
                };
````````````````````````````````````
void* rowCheck(void* arg){
    int* argument = (int*) arg;
    int idx = *argument;
    int count = 0;
    for(int i = 0; i < 9; i++){
        int temp = arr[idx][i];
        count = 0;
        for(int j = i; j < 9; j++){
            if(arr[idx][j] == temp || arr[idx][j] <= 0 || arr[idx][j] >= 10){
                count++; 
            }
            if(count > 1){
                pthread_mutex_lock(&mut1);
                count = 0;
                cout<<"ERROR at"<<arr[idx][j]<<endl;
                pthread_mutex_unlock(&mut1);
                break;
            }
        }
    }
    pthread_exit(NULL);
}

````````````````````````````````````
void* checkingRows(void* arg){
    int *row = new int;
    *row = 0;
    for(int i = 0; i<gridSize; i++){
        pthread_create(&workerIdRow[i], NULL, &rowCheck, row);
        *row = *row + 1;
    }
    pthread_exit(NULL);
}
`````````````````````````````````
int main(){

    pthread_mutex_init(&mut1, NULL);
    pthread_create(&id1, NULL, &checkingRows, NULL);
    pthread_join(id1,NULL);

    retrun 0;

}
````````````````````````````````````

ERROR at 6
ERROR at 6
布鲁诺

第一个创建的线程没用,因为你创建它并立即等待它的结束

相反,您不会等待其他 9 个线程的结束,因此您会在它们结束之前退出

当您启动子线程时,您没有同步以确保它们在为下一个线程修改它之前有时间读取行号,几个可以检查同一行。为此,您可以使用条件(仍然使用 C 线程库),或者您可以在每次给定索引的情况下提供一个新创建的int

你有内存泄漏,因为你没有删除int,实际上在你的情况下在堆中分配它是没用的


来自你的代码的建议,如你所见,我直接将相关行的地址提供给每个线程,我删除了中间线程并等待9个线程结束,更多一些其他更改

#include <pthread.h>
#include <iostream>

pthread_mutex_t mut1;

int arr[9][9] = {  
                    {6,2,4,5,3,9,1,8,7},
                    {6,6,9,7,2,8,6,3,4},
                    {8,3,7,6,1,4,2,9,5},
                    {1,4,3,8,6,5,7,2,9},
                    {9,5,8,2,4,7,3,6,1},
                    {7,6,2,3,9,1,4,5,8},
                    {3,7,1,9,5,6,8,4,2},
                    {4,9,6,1,8,2,5,7,3},
                    {2,8,5,4,7,3,9,1,6}
                };

void * rowCheck(void* arg)
{
    int * row = (int *) arg;
    const char * result = "ok";

    for(int i = 0; i < 9; i++){
      int temp = row[i];
      int count = 0;

      for(int j = i; j < 9; j++){
        if(row[j] == temp || row[j] <= 0 || row[j] >= 10){
          count++; 
        }
        if(count > 1){
          result = "ko";
          pthread_mutex_lock(&mut1);
          std::cout<<"ERROR at "<< row[j]
            << " (row " << (row - arr[0])/9
              << " column " << j << ")" << std::endl;
          pthread_mutex_unlock(&mut1);
          break;
        }
      }
    }
    pthread_exit((void *) result);
}

int main()
{
  pthread_t id[9];

  pthread_mutex_init(&mut1, NULL);

  for (int i = 0; i != 9; ++i) {
    if (pthread_create(&id[i], NULL, &rowCheck, arr[i]) != 0) {
      std::cerr << "error when creating thread " << i << std::endl;
      return -1;
    }
  }

  for (int i = 0; i != 9; ++i) {
    void * retval;

    pthread_join(id[i], &retval);

    pthread_mutex_lock(&mut1);
    std::cout << i << ":" << (char *) retval << std::endl;
    pthread_mutex_unlock(&mut1);
  }

  return 0;
}

编译和执行:

/tmp % g++ -pedantic -Wextra -Wall c.cc -lpthread
/tmp % ./a.out
ERROR at 6 (row 1 column 1)
ERROR at 6 (row 1 column 6)
0:ok
1:ko
2:ok
3:ok
4:ok
5:ok
6:ok
7:ok
8:ok

另一种方法是通过专用于每个线程的已分配int提供索引(并在线程中删除它):

#include <pthread.h>
#include <iostream>

pthread_mutex_t mut1;

int arr[9][9] = {  
                    {6,2,4,5,3,9,1,8,7},
                    {6,6,9,7,2,8,6,3,4},
                    {8,3,7,6,1,4,2,9,5},
                    {1,4,3,8,6,5,7,2,9},
                    {9,5,8,2,4,7,3,6,1},
                    {7,6,2,3,9,1,4,5,8},
                    {3,7,1,9,5,6,8,4,2},
                    {4,9,6,1,8,2,5,7,3},
                    {2,8,5,4,7,3,9,1,6}
                };

void * rowCheck(void* arg)
{
    int rowNum = *((int*) arg);

    delete (int*) arg;

    int * row = arr[rowNum];
    const char * result = "ok";

    for(int i = 0; i < 9; i++){
      int temp = row[i];
      int count = 0;

      for(int j = i; j < 9; j++){
        if(row[j] == temp || row[j] <= 0 || row[j] >= 10){
          count++; 
        }
        if(count > 1){
          result = "ko";
          pthread_mutex_lock(&mut1);
          std::cout<<"ERROR at "<< row[j]
            << " (row " << rowNum
              << " column " << j << ")" << std::endl;
          pthread_mutex_unlock(&mut1);
          break;
        }
      }
    }
    pthread_exit((void *) result);
}

int main()
{
  pthread_t id[9];

  pthread_mutex_init(&mut1, NULL);

  for (int i = 0; i != 9; ++i) {
    if (pthread_create(&id[i], NULL, &rowCheck, new int(i)) != 0) {
      std::cerr << "error when creating thread " << i << std::endl;
      return -1;
    }
  }

  for (int i = 0; i != 9; ++i) {
    void * retval;

    pthread_join(id[i], &retval);

    pthread_mutex_lock(&mut1);
    std::cout << i << ":" << (char *) retval << std::endl;
    pthread_mutex_unlock(&mut1);
  }

  return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章