simple multithreaded hello world

package main

import (
    "fmt"
    "time"
)

func main() {
    // Create a new thread
    go func() {
        for i := 1; i < 10; i++ {
            fmt.Printf("Hello World! %d. Printing from spawned thread\n", i);
            time.Sleep(1 * time.Millisecond)
        }        
    }()

    // Main thread    
    for i := 1; i < 10; i++ {
        time.Sleep(2 * time.Millisecond)
        fmt.Printf("Hello World! %d from the main thread\n", i)    
    }
}

0
0
Krish 100200 points

                                    use std::thread;
use std::time::Duration;

fn main() {
    // Create a new thread
    let handle = thread::spawn(|| {
        for i in 1..10 {
            println!(&quot;Hello World! {}. Printing from spawned thread&quot;, i);
            thread:: sleep(Duration::from_millis(1)); 
        }
    });

    // Main thread
    for i in 1..10 {
        println!(&quot;Hello World! {} from the main thread&quot;, i);
        thread::sleep(Duration::from_millis(2));
    }

    // Hold the main thread until the spawned thread has completed
    handle.join().unwrap();
}

0
0
0
0
Awgiedawgie 440215 points

                                    #include &lt;unistd.h&gt;  
#include &lt;pthread.h&gt;

// A C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp) {
    for (int i = 0; i &lt; 10; i++) {
        printf(&quot;Hello World! %d Printing from spawned thread\n&quot;, i);
        sleep(1);
    }
    return NULL;
}

int main() {
    // Create a new thread
    pthread_t thread_id;
    pthread_create(&amp;thread_id, NULL, myThreadFun, NULL);

    // Main thread
    for (int i = 0; i &lt; 10; i++) {
        printf(&quot;Hello World! %d from the main thread\n&quot;, i);
        sleep(2);
    }
    pthread_join(thread_id, NULL);
    exit(0);
}

0
0
0
8
Awgiedawgie 440215 points

                                    #include &lt;iostream&gt;
#include &lt;thread&gt;
#include &lt;chrono&gt;

using namespace std;

int main() {
    // Create a new thread
    auto f = [](int x) {
        for (int i = 0; i &lt; x; i++) {
            cout &lt;&lt; &quot;Hello World! &quot; &lt;&lt; i &lt;&lt; &quot; Printing from spawned thread&quot; &lt;&lt; endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
        }
    };
    thread th1(f, 10);

    // Main thread
     for (int i = 0; i &lt; 10; i++) {
        cout &lt;&lt; &quot;Hello World! &quot; &lt;&lt; i &lt;&lt; &quot; from the main thread&quot; &lt;&lt; endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
    // Wait for thread th1 to finish
    th1.join();
    return 0;
}

0
0
Are there any code examples left?
New code examples in category Go
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source