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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:47:49+00:00 2026-05-27T01:47:49+00:00

Here is my code: static long x = 0; void * thread_func(void *arg){ while(1){

  • 0

Here is my code:

static long x = 0;
void * thread_func(void *arg){
while(1){
    printf("Thread 2 says %ld\n",++x);
    sleep(1);
}
}
int main(){
pthread_t tid;
pthread_create(&tid,NULL,thread_func,NULL);
while(x < 10)   {
    printf("Thread 1 says %ld\n",++x);
    sleep(2);
}
return 0;
}

Now what shall be the output, the book says it will be:

OUTPUT
Thread 1 says 1
Thread 2 says 2
Thread 2 says 3
Thread 1 says 4
Thread 2 says 5
Thread 2 says 6
Thread 1 says 7
Thread 2 says 8
Thread 2 says 9
Thread 1 says 10
Thread 2 says 11
Thread 2 says 12

Now shall not the first one should be Thread 2 says 1, since the pthread_create() is before the while loop in main.
Also how does sleep work here?
Like what exactly is a thread sleeping for, and what process executes when the executing thread sleeps.?

i.e sleep(1) and sleep(2) what does 1 and 2 signify?

  • 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-27T01:47:50+00:00Added an answer on May 27, 2026 at 1:47 am

    I am not sure what book you are reading, but you should stop reading it immediately. First of all, you cannot guarantee that thread will be up and running before entering a loop in main. It could start before or after. Second of all, there is no access synchronization when accessing variable x from two different threads, thus no guarantee as for what value it will have. Calling printf synchronizes on stdout object, and calling sleep swaps the thread out, and it takes a lot of time to schedule it again. This example is useless and assumptions about the output are wrong.

    Now, some answers..

    Now shall not the first one should be Thread 2 says 1, since the
    pthread_create() is before the while loop in main.

    Not necessarily. It takes hell of a lot CPU cycles to start another thread. So it is most likely that you will enter while loop in main before that other thread starts. But, not necessarily.

    Also how does sleep work here?

    It tells the kernel not to execute the calling thread for a given number of seconds. The actual sleeping time could be a bit longer due to overhead associated with scheduling. Other threads are not affected by that (well, they are, but not directly, i.e. other threads can have more CPU time etc.).

    what process executes when the executing thread sleeps?

    Process never executes anything. It is the kernel’s schedule that executes processes (and threads). By default, process consists of one main thread, and kernel executes it. When you create more threads, scheduler executes more threads inside the process etc. When one or more threads are sleeping, schedulers executes those not sleeping. Think of the threads as of processes sharing the same address space – that’s what they are.

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

Sidebar

Related Questions

Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Here is my code : public static void main(String[] args) { // System.setProperty( //
Here's the sample code: public static void col (int n) { if (n %
Here's the relevant code: public static void printBoarders (Territory x) { int t =
Here is the code: class Time { public static void printTime (int hour, int
here is the original code: public static int getInt () { Scanner in =
I have following code: public static void ProcessStep(Action action) { //do something here if
const static int foo = 42; I saw this in some code here on
here is the code (java): class prime { public static boolean prime (int a,
Here's the code: class qual { public static int fibonacci(int n) { if (n

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.