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 7968505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:05:18+00:00 2026-06-04T07:05:18+00:00

I have 2 thread to create thread1 and thread2. When creating threads with: pthread_create(&thread1,

  • 0

I have 2 thread to create thread1 and thread2. When creating threads with:

pthread_create(&thread1, NULL, &function_th1, NULL);
pthread_create(&thread2, NULL, &function_th2, NULL);

The thread2 is launched before the thread1 and I’m looking for a solution to start thread1 before thread2.

Not looking for this solution:

pthread_create(&thread2, NULL, &function_th2, NULL);
pthread_create(&thread1, NULL, &function_th1, NULL);
  • 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-06-04T07:05:19+00:00Added an answer on June 4, 2026 at 7:05 am

    here after the solution I suggest

    #include <pthread.h>
    #include <stdio.h>
    
    static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
    static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    static bool wait = TRUE;
    
    void thread_sync() {
      pthread_mutex_lock(&mut);
      wait = FALSE;
      pthread_cond_signal(&cond);
      pthread_mutex_unlock(&mut);
    }
    void thread_wait_sync() {
      pthread_mutex_lock(&mut);
      if (wait==TRUE)
      {
          pthread_cond_wait(&cond,&mut);
      }
      wait = TRUE;
      pthread_mutex_unlock(&mut);
    }
    
    void *thread1(void *d) {
      thread_sync();
      while (1); // Rest of work happens whenever
      return NULL;
    }
    
    void *thread2(void *d) {
      thread_sync();
      while (1);
      return NULL;
    }
    
    void *thread3(void *d) {
      thread_sync();
      while (1);
      return NULL;
    }
    
    void *thread4(void *d) {
      while (1);
      return NULL;
    }
    
    int main() {
      pthread_t t1,t2,t3,t4;
      pthread_create(&t1, NULL, thread1, NULL);
      thread_wait_sync();
      pthread_create(&t2, NULL, thread2, NULL);
      thread_wait_sync();
      pthread_create(&t3, NULL, thread3, NULL);
      thread_wait_sync();
      pthread_create(&t4, NULL, thread4, NULL);
      while(1) {
        // some work
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

assume creating 3 worker threads by pthread_create, in these worker thread routine, each call
I have a process that must create and close threads on demand. Each thread
I have two requests in tomcat. One HTTP request will create a thread. Client
I have a req where in i have to create a worker thread and
I have a custom thread pool class, that creates some threads that each wait
I have a parent thread (non-UI) which creates some child threads to do some
I have a parent thread (non-UI) which creates some child threads to do some
I have a class,which implements Runnable interface. I want to create multiple threads for
I have two questions about creating thread safe types in python, and one related
Using POSIX threads & C++, I have an Insert operation which can only be

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.