calling method from a new thread C

Joel Shevin

I'm working on a project (NOT HOMEWORK), building a multi-thread sudoku solution validator in C. I'm new to C so excuse the bad code quality as I'm still improving.

I want to call the method row_check 9 times from 9 separate threads. For the method as parameters I pass the row number (arg) and array name (arr). I have created the thread but I'm unsure how to pass the parameters properly to the method. Can anyone help me with this?

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


void* row_check(void* arg, int *arr)
{
    int i = *((int *)arg); //trying to convert row number to int
    int j, flag;

    while(i < 9)
    {
        flag=0x0000;

        for(j = 0; j < 9; j++)
            flag |= 1 << (arr[i][j]-1);

        if (flag != 0x01FF)
            report("row", i, j-1);
    }

}

void report(char *s, int i, int j)
{
   printf("\nThe sudoku is INCORRECT");
   printf("\nin %s. Row:%d,Column:%d", s, i+1, j+1);
   getch();
   exit(0);
 }


int main(int argc, char* argv[])
{
     int i,j;
     char arr1[9][9];
     FILE *file = fopen(argv[1], "r");

     if (file == 0)
     {
       fprintf(stderr, "failed");
       exit(1);
     }
      int col=0, row=0;
      int num;

      while(fscanf(file, "%d ", &num) == 1)
      {
         arr1[row][col] = num;
         col++;
         if(col == 9)
         {
            row++;
            col = 0;
         }
      }
      fclose(file);

      pthread_t tid;
      pthread_attr_t attr; 
      pthread_attr_init(&attr);

      int n;
      for(n=0; n < 9; n++) //creating 9 threads
      {
          pthread_create(&tid, &attr, row_check, n);
          pthread_join(tid, NULL);
      }

      return 0;
}
Alex Lop.

The thread entry function has to be of the format void *(*start_routine) (void *), which means it receives only one parameter - pointer to anything you like.

The most used technique is to define a struct with the values you want to pass to the thread entry function. Create a variable of that type, initialize it and pass its address to the thread entry function.

Example:

typedef thread_data_s
{
    char *ptr;
    int   row_num; // I would prefer to define it as `unsigned int` but I stick to your example
    // + any other data you want to pass to the thread
} thread_data_t;

....

thread_data_t data[NUM_OF_THREADS];

....

for(n=0; n < NUM_OF_THREADS; n++) //creating 9 threads
{
     data[n].ptr = &arr1[n][0];
     data[n].row_num = n;
     pthread_create(&tid, &attr, row_check, &data[n]);
}

...

for(n=0; n < NUM_OF_THREADS; n++) // waiting for all the threads here
{
     pthread_join(tid, NULL);
}

And your entry function should look something like this:

void* row_check(void* data)
{
    //int i = *((int *)arg); //trying to convert row number to int
    thread_data_t *my_data_ptr = data;
    int j, flag;

    while(i < 9)
    {
        flag=0x0000;

        for(j = 0; j < 9; j++)
            flag |= 1u << ( (my_data_ptr->ptr)[my_data_ptr->row_num][j] - 1 );
        // Shouldn't it be under the `for` loop block? If so, please add `{}` to the `for` loop
        if (flag != 0x01FF)
            report("row", my_data_ptr->row_num, j-1);
    }

    return NULL;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a method from the class that created a thread, in the thread

From Dev

Calling a method in a running thread, from outside the thread

From Dev

Calling a method in thread from another thread, python

From Dev

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

From Dev

c++ std::thread calling method from object cause to calling destructor of this class

From Dev

Calling java method from different thread in ndk

From Dev

Calling a static C# method from a managed C++/CLI thread

From Java

Main thread is blocked when calling method from another thread

From Dev

Does calling an async method within BeginInvoke() spawn a 'new' thread?

From Dev

Passing UI Thread method to another thread for calling in C#

From Dev

Is calling a static method from an instance method thread safe?

From Dev

Calling to a pointer from a c++ thread

From Dev

Calling a method on the main thread?

From Dev

calling C++ method from C code

From Dev

Calling a method from inside a thread to another class in python pyqt

From Dev

What is the scope of "this" when calling a method from an individual thread?

From Java

Calling parent method from within a Thread having the same name

From Dev

Calling thread from test method :code is optimized or a native frame is on top

From Dev

Multiple processes in method executed asynchronously from calling thread

From Dev

Invoke method in new thread (method name from string)?

From Dev

Why does the priority from getPriority inside a new thread differ from the priority in the calling thread?

From Java

Calling a java method from c++ in Android

From Dev

Calling a C# method from JavaScript

From Dev

calling objective c method from callback

From Dev

Calling Rust method from C with array parameters

From Dev

Calling C++ method with callback from ObjectiveC

From Dev

c++ calling method from subclass in superclass

From Java

calling new thread inside is constructor

From Dev

Calling a New Method from a Different Class in the Same Package

Related Related

  1. 1

    Calling a method from the class that created a thread, in the thread

  2. 2

    Calling a method in a running thread, from outside the thread

  3. 3

    Calling a method in thread from another thread, python

  4. 4

    Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

  5. 5

    c++ std::thread calling method from object cause to calling destructor of this class

  6. 6

    Calling java method from different thread in ndk

  7. 7

    Calling a static C# method from a managed C++/CLI thread

  8. 8

    Main thread is blocked when calling method from another thread

  9. 9

    Does calling an async method within BeginInvoke() spawn a 'new' thread?

  10. 10

    Passing UI Thread method to another thread for calling in C#

  11. 11

    Is calling a static method from an instance method thread safe?

  12. 12

    Calling to a pointer from a c++ thread

  13. 13

    Calling a method on the main thread?

  14. 14

    calling C++ method from C code

  15. 15

    Calling a method from inside a thread to another class in python pyqt

  16. 16

    What is the scope of "this" when calling a method from an individual thread?

  17. 17

    Calling parent method from within a Thread having the same name

  18. 18

    Calling thread from test method :code is optimized or a native frame is on top

  19. 19

    Multiple processes in method executed asynchronously from calling thread

  20. 20

    Invoke method in new thread (method name from string)?

  21. 21

    Why does the priority from getPriority inside a new thread differ from the priority in the calling thread?

  22. 22

    Calling a java method from c++ in Android

  23. 23

    Calling a C# method from JavaScript

  24. 24

    calling objective c method from callback

  25. 25

    Calling Rust method from C with array parameters

  26. 26

    Calling C++ method with callback from ObjectiveC

  27. 27

    c++ calling method from subclass in superclass

  28. 28

    calling new thread inside is constructor

  29. 29

    Calling a New Method from a Different Class in the Same Package

HotTag

Archive