Passing an element of an array (struct) through pthread_create

user8070337

I have some issue with passing an element of an array through pthread_create as paramenter.

I have this struct:

struct threadInfo {
   int threadNumber;
   int sleepTime;
};

I initialize the array (in a function) like this:

struct threadInfo info[1];

Then in a while-loop I do this:

int i = 0;
...
while (i < 2) {
        pthread_mutex_lock(&countMutex);
        if (threadsCount < MAX_THREAD) {
                info[i].threadNumber = ++threadsCount;
        pthread_mutex_unlock(&countMutex);
                info[i].sleepTime = rand() % (10 + 1 - 1) + 1; 
                pthread_create(&threads[i], NULL, lawine, &info[i]);
                i++;
        }
        else {
                pthread_mutex_unlock(&countMutex);
                break;
        }
    }

threadsCount is a global var.

In the first turn it works fine(info[0]). But In the second round (info1) the values are wrong. the output

Can you help me?

Chris Turner

You're going out of bounds on your array as you've only declared it to hold one element, but you're expecting it to hold 2. The number inside the square brackets in this line of code...

struct threadInfo info[1];

... isn't the highest index you can access. It's the number of elements, so if you want to have 2 elements (and thus be able to access info[1]) you need to allocate it like so:

struct threadInfo info[2];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing malloc struct array through a function

From Dev

Passing strings through and array to a struct in C

From Dev

Passing a Struct Array through a TableViewCell to another ViewController

From Dev

filenames not passing into pthread_create

From Dev

Passing char *** at pthread_create

From Dev

Printing Array Values Passed Thru a Struct Via Pthread_Create in C++

From Dev

Passing variadic template to pthread_create

From Dev

Problem about passing an array in struct when using pthread

From Dev

Passing an array through filter does not narrow element type

From Dev

How to iterate through an array struct and return the element I want in pyspark

From Dev

Is it ever possible to create a reference to element of array of struct?

From Dev

Array Passing through javascript

From Dev

Passing a contenteditable element through form?

From Dev

Correctly passing an object to pthread_create() using void*

From Java

pthread_create not working. passing argument 3 warning

From Dev

Issue with POSIX thread synchronization and/or pthread_create() argument passing

From Dev

Pass a struct to pthread_create's startup routine

From Dev

Struct loses track of variables when using pthread_create

From Dev

Passing an array of 'typedef struct' to a function

From

passing a slice/array to another struct

From Dev

passing array (not pointer) to a struct in c

From Dev

Passing entire struct array to function

From Dev

Passing array with fixed size to struct

From Dev

Passing a struct to construct an array of Structs

From Dev

Passing array of struct to function in C

From Dev

passing struct array as parameter to a function

From Dev

Unflatten struct after passing through UDP socket

From Dev

Getting a warning when passing struct through function

From Dev

Passing a struct to a process through shared memory in C

Related Related

  1. 1

    Passing malloc struct array through a function

  2. 2

    Passing strings through and array to a struct in C

  3. 3

    Passing a Struct Array through a TableViewCell to another ViewController

  4. 4

    filenames not passing into pthread_create

  5. 5

    Passing char *** at pthread_create

  6. 6

    Printing Array Values Passed Thru a Struct Via Pthread_Create in C++

  7. 7

    Passing variadic template to pthread_create

  8. 8

    Problem about passing an array in struct when using pthread

  9. 9

    Passing an array through filter does not narrow element type

  10. 10

    How to iterate through an array struct and return the element I want in pyspark

  11. 11

    Is it ever possible to create a reference to element of array of struct?

  12. 12

    Array Passing through javascript

  13. 13

    Passing a contenteditable element through form?

  14. 14

    Correctly passing an object to pthread_create() using void*

  15. 15

    pthread_create not working. passing argument 3 warning

  16. 16

    Issue with POSIX thread synchronization and/or pthread_create() argument passing

  17. 17

    Pass a struct to pthread_create's startup routine

  18. 18

    Struct loses track of variables when using pthread_create

  19. 19

    Passing an array of 'typedef struct' to a function

  20. 20

    passing a slice/array to another struct

  21. 21

    passing array (not pointer) to a struct in c

  22. 22

    Passing entire struct array to function

  23. 23

    Passing array with fixed size to struct

  24. 24

    Passing a struct to construct an array of Structs

  25. 25

    Passing array of struct to function in C

  26. 26

    passing struct array as parameter to a function

  27. 27

    Unflatten struct after passing through UDP socket

  28. 28

    Getting a warning when passing struct through function

  29. 29

    Passing a struct to a process through shared memory in C

HotTag

Archive