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

struct Arg1{
    int x,y;
};
void * process(void * threadarg){
    int x,y;
    Arg1 * myArg1;
    myArg1 = (Arg1 *) threadarg;
    x = myArg1->x;
    y=myArg1->y;
    int i=0;
    while(i<500){
        x+=1;
        y+=2;
        i++;
    }
    myArg1->x=x;
    myArg1->y=y;
    pthread_exit(NULL);
}
using namespace std;

int main(){
    int x = 0;
    int y = 0;
    int n;
    cin>>n;
    Arg1 sharedObj;
    sharedObj.x=x;
    sharedObj.y=y;
    auto start = chrono::high_resolution_clock::now();
    pthread_t *threads = new pthread_t[n];
    for(int i=0;i<n;++i){
        pthread_create(&threads[i],NULL,process,(void *)&sharedObj);
    }
    for(int i=0;i<n;++i){
        pthread_join(threads[i],NULL);
    }

    cout<<sharedObj.x<<" "<<sharedObj.y<<"\n";
    auto stop = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::microseconds>(stop-start);
    cout<<duration.count()<<"\n";
}

I wrote this code to see if using shared variables cause problems in the result.

What it does is this- It first asks the user for number of threads n. Each thread increments shared variables x and y by 1 and 2 respectively. Variables are shared via the use of the same struct object. In the end I print the values of x and y from the struct object.

I saw that for a small while loop in the function process, I am able to get correct results, regardless the number of threads n. However, for large values, about 5000, I get wrong results sometimes.

Also, the time of execution increases with the number of threads, and I don't understand whether this is because of improper multithreading or because of overhead in creating more number of threads.

Thanks for any help in advance.

levio_sa
  • 123
  • 9
  • If you have more CPU bound threads than your computer has available physical threads then the scheduler has to swap the logical threads into and out of the CPU. There is of cause an overhead in doing this (the state of the thread needs to be saved/restored). This will therefore decrease the amount of useful work being performed as overhead increases (more threads). With regard to reading and writing shared variable(s) without any synchronization this is Undefined Behaviour. – Richard Critten Mar 25 '21 at 13:20
  • 1
    Undefined behavior for unsynchronized, non-readonly, non-atomic access to an object form multiple threads. Also, you have multiple memory leaks and you're mixing C and C++ in a way that is *extremely* inadvisable, so I would recommend you first try learning single-threaded programming before you attempt multithreading. – EOF Mar 25 '21 at 15:40
  • Could you be kind enough to point out where are some memory leaks and why does the code seem to be mixing C and C++? Is it because of so many pointers used by me? I did so as this was the only kind of implementation using pthreads that I found. – levio_sa Mar 26 '21 at 03:17
  • 1
    _"...mixing C and C++..."_ in C++ you could be using `std::thread` for threading and `std::vector` instead of the dynamic array. Only memory leak I can see is of `threads`. – Richard Critten Mar 26 '21 at 10:25
  • And I thought I was implementing something closer to architecture(low level) so I would create a better program. Using `std::vector` and `std::thread` seemed to be a lazy approach. Thanks for pointing it out. – levio_sa Mar 26 '21 at 14:27

1 Answers1

0

Unless we use a mutex or some method to ensure atomicity of write, we can not guarantee that we would get a correct value in pthreaded applications. This is because, simultaneous writes can lead to a race condition, with only one of the write affecting the shared variable.

levio_sa
  • 123
  • 9