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

  • SEARCH
  • Home
  • 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 5951745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:34:53+00:00 2026-05-22T17:34:53+00:00

I have searched for many hours for a solution, but cannot find an easy

  • 0

I have searched for many hours for a solution, but cannot find an easy answer.
I got a class, which uses pthreads. The actual function pointer is static within the class, and I need to lock on a mutex, because so far I get “weird” results (parameters not being passed correctly).

However the pthread_mutex_lock and unlock will not work within the function given to the thread, because it is in a static member function, yet I cannot have the function non static because it won’t work inside the class, and I cannot move it outside the class, because it won’t be able to access required info.

The following code should explain:

class Fight{

     pthread_mutex_t thread_mutex;
     static void *thread_run_fighter(void *temp);

  public:

    Fight();
    bool thread_round(Individual &a, int a_u, Individual &b, int b_u);
    std::vector<Individual> tournament();
  };

And the cpp file:

    Fight::Fight(){
       thread_mutex = PTHREAD_MUTEX_INITIALIZER;
    }

    bool Fight::thread_round(Individual &a, int a_u, Individual &b, int b_u){

    if (a.saved and b.saved){
       a.uniform = a_u;
       b.uniform = b_u;
       Individual *one = &a;
       Individual *two = &b;      
       pthread_t a_thread, b_thread;
       int a_thread_id, b_thread_id;
       a_thread_id = pthread_create(&a_thread,NULL,Fight::thread_run_fighter,(void*) one);
       b_thread_id = pthread_create(&b_thread,NULL,Fight::thread_run_fighter,(void*) two);

       pthread_join( a_thread, NULL);
       pthread_join( b_thread, NULL); 
       return true;
    }
    else{
       return false;
    }
   }

   void *Fight::thread_run_fighter(void *temp){

     Individual *indiv;
     indiv = (class Individual*)temp;
     pthread_mutex_lock( &thread_mutex );
     indiv->execute(indiv->uniform);
     pthread_mutex_unlock( &thread_mutex );

   }

I would be really grateful if anyone could shed some light into this. I have been stuck for some hours now, and I could not find any info whatsoever.
Thank you!

  • 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-22T17:34:54+00:00Added an answer on May 22, 2026 at 5:34 pm

    By ‘doesn’t work’ I assume you mean it won’t compile since you’re trying to use an instance member in a static member function.

    But the bigger question is why are you trying to use threads for this?

    The thread function you have is entirely protected by the mutex anyway – you’ll get the same (or better) performance by simply calling

    a.execute(a.uniform);
    b.execute(b.uniform);
    

    instead of spinning up the threads then waiting for them to complete.


    But if you really want to use threads (maybe you’re learning about them) and you want your static member function to be able to deal with instance members, here are some pointers. To get that to work, you’ll need to somehow pass an instance of the Fight object to the static thread function:

    // somewhere in `class Fight` definition:
    //
    // a structure that will let you pass a Fight* instance pointer
    //  along with an Individual* to work on in the the
    //  thread function
    
    struct context {
        Fight* me;
        Individual* indiv;
    };
    
    
    
    // ...
    
    // in Fight::thread_round():
    //  package up data to pass to the thread function
    context one = {this, &a };  // instead of Individual *one = &a;
    context two = {this, &b };  // instead of Individual *two = &b;
    

    Finally, Fight::thread_run_fighter():

    void *Fight::thread_run_fighter(void *temp)
    {
        // pull out the Fight object instance and the Individual
        //  object to work on
        context* ctx = (context*) temp;
        Individual *indiv = ctx->indiv;
        Fight* me = ctx->me;
    
        // do the work (in a pretty serialized fashion, unfortunately)
        pthread_mutex_lock( &me->thread_mutex );
        indiv->execute(indiv->uniform);
        pthread_mutex_unlock( &me->thread_mutex );
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched many threads so far but cant seem to find a solution.
Very basic, and very annoying, I have searched solution for many hours with no
I have searched on the net for a couple of hours. I got many
I have searched many topics and didn't find the answer, or question was too
I have searched through many times but have not seen this before. Probably really
I realise this may be a duplicate, but I have searched through many forums
I have searched through the google and also joomla forums but didn't got what
I have searched many places to add and display images dynamically on JPanel but
I have searched through the forum for my answer, But all the calendar questions
I want to use bar chart in web application. I have searched many libraries

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.