engineer-man / piston

A high performance general purpose code execution engine.
https://emkc.org/run
MIT License
1.95k stars 252 forks source link

cpp thread library issue #665

Open ddoddii opened 6 months ago

ddoddii commented 6 months ago

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"
    }
}