0

I've got the following example, let's say I want for each thread to count from 0 to 9.

void* iterate(void* arg) {
    int i = 0;
    while(i<10) {
        i++;
    }
    pthread_exit(0);
}

int main() {
    int j = 0;        
    pthread_t tid[100];
    while(j<100) {
        pthread_create(&tid[j],NULL,iterate,NULL);
        pthread_join(tid[j],NULL);
    }
}

variable i - is in a critical section, it will be overwritten multiple times and therefore threads will fail to count.

int* i=(int*)calloc(1,sizeof(int));

doesn't solve the problem either. I don't want to use mutex. What is the most common solution for this problem?

5
  • 6
    You don't have a problem. Each thread has it's own local i variable. It isn't shared between them, at all. Commented Feb 10, 2018 at 17:57
  • 3
    Variable i is a local variable, which means there's a separate i for each thread. Also, you're only running one thread at a time anyways. By calling pthread_join after pthread_create, you force main to wait for one thread to finish before creating the next thread. Not to mention the fact that you never increment j, so the loop is infinite. Commented Feb 10, 2018 at 17:57
  • so, if i decide to initialize a new variable inside a thread, then thread gets its own variable with unique address, and which can't be accessed by any other thread? Commented Feb 10, 2018 at 18:13
  • 1
    @ArtEm, true for local variables, not for file or global scope. Commented Feb 10, 2018 at 18:34
  • Did you intend for i to be shared between the threads? Commented Feb 10, 2018 at 18:41

1 Answer 1

1

As other users are commenting, there are severals problems in your example:

  1. Variable i is not shared (it should be a global variable, for instance), nor in a critical section (it is a local variable to each thread). To have a critical section you should use locks or transactional memory.

  2. You don't need to create and destroy threads every iteration. Just create a number of threads at the beggining and wait for them to finish (join).

  3. pthread_exit() is not necessary, just return from the thread function (with a value).

  4. A counter is a bad example for threads. It requires atomic operations to avoid overwriting the value of other threads. Actually, a multithreaded counter is a typical example of why atomic accesses are necessary (see this tutorial, for example).

I recommend you to start with some tutorials, like this or this.

I also recommend frameworks like OpenMP, they simplify the semantics of multithreaded programs.

EDIT: example of a shared counter and 4 threads.

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

#define NUM_THREADS 4

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static int counter = 0;

void* iterate(void* arg) {
    int i = 0;
    while(i++ < 10) {
        // enter critical section
        pthread_mutex_lock(&mutex);
        ++counter;
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}

int main() {
    int j;        
    pthread_t tid[NUM_THREADS];
    for(j = 0; j < NUM_THREADS; ++j)
        pthread_create(&tid[j],NULL,iterate,NULL);

    // let the threads do their magic

    for(j = 0; j < NUM_THREADS; ++j)
        pthread_join(tid[j],NULL);

    printf("%d", counter);

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.