Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpp thread library issue #665

Open
ddoddii opened this issue May 14, 2024 · 0 comments
Open

cpp thread library issue #665

ddoddii opened this issue May 14, 2024 · 0 comments

Comments

@ddoddii
Copy link

ddoddii commented May 14, 2024

I tried running this simple cpp code that contains thread library, but it gives me error. std::thread library is included in c++ since c++11. I saw issue opened including c++17, but I don't know why executing this doesn't work.

Anyone having the same issue ?

#include <iostream>
#include <thread>
#include <vector>
using namespace std;
/*
Race condition because of multi-threads using the same resource
*/

void worker(int *input, int start, int size, int *output)
{
    for (int i = 0; i < size; i++)
    {
        if (input[start + i] == 0)
        {
            (*output)++;
        }
    }
}

int main()
{
    const int N = 100000;
    const int NT = 3;

    int *array = new int[N];
    for (int i = 0; i < N; i++)
    {
        array[i] = 0;
    }

    int count = 0;
    vector<thread> threads;
    for (int t = 0; t < NT; t++)
    {
        int size = N / NT;
        int start = t * size;
        threads.push_back(thread(worker, array, start, size, &count));
    }
    for (auto &thread : threads)
    {
        thread.join();
    }

    cout << "There are " << count << " zeros" << endl;
}
/piston/packages/gcc/10.2.0/run: line 6: ./a.out: No such file or directory

I tried using postman request with the same code, and this is the full error

{
    "language": "c++",
    "version": "10.2.0",
    "run": {
        "stdout": "",
        "stderr": "/piston/packages/gcc/10.2.0/run: line 6: ./a.out: No such file or directory\n",
        "code": 127,
        "signal": null,
        "output": "/piston/packages/gcc/10.2.0/run: line 6: ./a.out: No such file or directory\n"
    },
    "compile": {
        "stdout": "",
        "stderr": "/usr/bin/ld: /tmp/ccmji35F.o: in function `std::thread::thread<void (&)(int*, int, int, int*), int*&, int&, int&, int*, void>(void (&)(int*, int, int, int*), int*&, int&, int&, int*&&)':\nmain.cpp.cpp:(.text._ZNSt6threadC2IRFvPiiiS1_EJRS1_RiS5_S1_EvEEOT_DpOT0_[_ZNSt6threadC5IRFvPiiiS1_EJRS1_RiS5_S1_EvEEOT_DpOT0_]+0x37): undefined reference to `pthread_create'\ncollect2: error: ld returned 1 exit status\nchmod: cannot access 'a.out': No such file or directory\n",
        "code": 1,
        "signal": null,
        "output": "/usr/bin/ld: /tmp/ccmji35F.o: in function `std::thread::thread<void (&)(int*, int, int, int*), int*&, int&, int&, int*, void>(void (&)(int*, int, int, int*), int*&, int&, int&, int*&&)':\nmain.cpp.cpp:(.text._ZNSt6threadC2IRFvPiiiS1_EJRS1_RiS5_S1_EvEEOT_DpOT0_[_ZNSt6threadC5IRFvPiiiS1_EJRS1_RiS5_S1_EvEEOT_DpOT0_]+0x37): undefined reference to `pthread_create'\ncollect2: error: ld returned 1 exit status\nchmod: cannot access 'a.out': No such file or directory\n"
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant