Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've wrote this code:

#include <iostream>
#include <thread>

using namespace std;

#define ll long long
#define ull unsigned long long
#define ld long double

const int mx = 1e9;

void f(int l, int r, int *res)
{
    int c_3, c_5;
    c_3 = c_5 = 0;
    for (int i = l; i < r; i++)
    {
        if (!(i % 3)) c_3++;
        if (!(i % 5)) c_5++;
    }

    *res = c_3 + c_5;
}


void s(int l, int r, int *res)
{
    int c_3, c_5;
    c_3 = c_5 = 0;
    for (int i = l; i < r; i++)
    {
        if (!(i % 3)) c_3++;
        if (!(i % 5)) c_5++;
    }

    *res = c_3 + c_5;
}


int main()
{
    int res1, res2;

    thread th1(&f, 0, mx / 2, &res1);
    thread th2(&s, mx / 2, mx, &res2);

    th1.join();
    th2.join();

    cout << res1 + res2;

    return 0;
}



And I have following error:

/usr/bin/ld: /tmp/cc3k2P1B.o: in function `std::thread::thread<void (*)(int, int, int*), int, int, int*, void>(void (*&&)(int, int, int*), int&&, int&&, int*&&)':
main.cpp:(.text._ZNSt6threadC2IPFviiPiEJiiS1_EvEEOT_DpOT0_[_ZNSt6threadC5IPFviiPiEJiiS1_EvEEOT_DpOT0_]+0x43): undefined reference to `pthread_create'
/usr/bin/ld: /tmp/cc3k2P1B.o: in function `std::thread::thread<void (*)(int, int, int*), int, int const&, int*, void>(void (*&&)(int, int, int*), int&&, int const&, int*&&)':
main.cpp:(.text._ZNSt6threadC2IPFviiPiEJiRKiS1_EvEEOT_DpOT0_[_ZNSt6threadC5IPFviiPiEJiRKiS1_EvEEOT_DpOT0_]+0x43): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status


Can you help me?

What I have tried:

I've tried give to the thread function "f" or "&f" and else.
Posted
Updated 25-Sep-21 2:30am
Comments
Greg Utas 25-Sep-21 8:26am    
It looks like thread's constructor uses pthread_create but that the compiler can't find pthread_create. I haven't used Linux, so that's all I can tell you. You need to find out where pthread_create is declared and make sure the compiler knows to include its directory when looking for files needed during your build.
temchik_ggg 25-Sep-21 8:46am    
Thx
merano99 27-Sep-21 18:33pm    
The function pthread_create looks like POSIX. Since you marked the code as Linux, you should link with -lpthread and maybe you need -lm as well.

1 solution

Did you include -pthread in your compile options as stated at pthread_create(3) - Linux manual page[^].
 
Share this answer
 
Comments
CPallini 25-Sep-21 10:44am    
5.
Richard MacCutchan 25-Sep-21 11:36am    
Thanks. I continue to be amazed at how few 'developers' think of looking at the documentation; or indeed in many cases, even thinking.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900