Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 528727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:01:01+00:00 2026-05-13T09:01:01+00:00

I have the following test program. #include <iostream> #include <cstdlib> using namespace std; pthread_mutex_t

  • 0

I have the following test program.

#include <iostream>
#include <cstdlib>

using namespace std;    
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char *argv[])
{
  int iret;
  iret = pthread_mutex_trylock( & mymutex );
  cout << "Test2 !!! " << endl;
  pthread_mutex_unlock( & mymutex );
  return EXIT_SUCCESS;
}

If I compile it without adding the pthread library I get the error for unresolved error for pthread_mutex_trylock , but only for function pthread_mutex_trylock.

If I replace pthread_mutex_trylock with pthread_mutex_trylock the program is compiled and runnig well also without the -lpthread* option.

If I add the -lpthraed option to the compile commands all runs well
this run well : $ g++ test2.c -o test2 -lpthread
this warn unresolved : $ g++ test2.c -o test2

Example error output:
$ g++ test2.c -o test2
/tmp/ccU1bBdU.o: In function main':
test2.c:(.text+0x11): undefined reference to
pthread_mutex_trylock’
collect2: ld returned 1 exit status

If I replace the instruction iret = pthread_mutex_trylock( & mymutex );

with iret = pthread_mutex_lock( & mymutex ); the program compiles and run without error also if didn’t add the pthread libarry to the compile command
I know that is right to have the unresolved error if I didn’t use the -lpthread option , but why I have not the same unresolved error also for other pthread_ function ?

I’m using gcc 4.4.2 on fedora 12

$ g++ --version
g++ (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20)
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Do some have some suggestion about the meaning of this unreference only for pthread_mutex_trylock ?

thanks for the help, Enzo

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:01:01+00:00Added an answer on May 13, 2026 at 9:01 am

    If you use pthread functions you should link your object files with -lpthread and not worry about whether symbols are included in libc.

    The rationale behind this is said to be such: some time ago the stubs in libc were used when application that used threads was run on a system without threading support. On such system, pthread_* functions became linked to libc stubs that returned errors showing that there’s no threading functionality. While on “threaded” systems they were linked to pthread library and worked correctly.

    Obviously, pthread_mutex_trylock function appeared after the policy changed to linking with -lpthread. So there’s no stub for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This should do the trick. <activity ... android:launchMode="singleTop" /> When… May 13, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer If you can view the files by hitting the URL… May 13, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer It looks like the SQL Server doesn't have permission to… May 13, 2026 at 10:45 pm

Related Questions

I'm new to C++, this is my first week since the upgrade from fortran.
I am currently investigating how Thread.Interrupt plays together with P/Invoke or native calls. I
I am trying to debug C++ code using Eclipse Galileo on my MacBook Pro
So let's say you want to build the Boost Getting Started examples and link
I had encountered a problem while using Loki::Singleton , Loki::SmartPtr , and std::vector under

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.