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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:21:57+00:00 2026-06-11T11:21:57+00:00

I’m trying to learn how Threads and mutexes work however I’m running into a

  • 0

I’m trying to learn how Threads and mutexes work however I’m running into a bit of a confusion hole right now, I took the following code from the official SFML 1.6 tutorials:

#include <SFML/System.hpp>
#include <iostream>

void ThreadFunction(void* UserData)
{
    // Print something...
    for (int i = 0; i < 10; ++i)
        std::cout << "I'm the thread number 1" << std::endl;
}

int main()
{
    // Create a thread with our function
    sf::Thread Thread(&ThreadFunction);

    // Start it !
    Thread.Launch();

    // Print something...
    for (int i = 0; i < 10; ++i)
        std::cout << "I'm the main thread" << std::endl;

    return EXIT_SUCCESS;
}

And it said

So the text from both threads will be displayed at the same time.

However that’s not happening, it first executes the first thread then the second thread, aren’t they supposed to run at the same time? I’m using Codeblocks IDE on Windows XP SP3, running SFML 1.6. Am I doing something wrong, or have I misunderstood how they work? From my point of view, threads are supposed to execute at the same time, so the output should be something like

“text from thread 1
text from thread 2
text from thread 1
and so on”

enter image description here

  • 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-11T11:21:58+00:00Added an answer on June 11, 2026 at 11:21 am

    … aren’t they supposed to run at the same time?

    Well, that depends.

    If you have two or more cores, they may run concurrently.

    Even if you have the available hardware, it’s up to your OS to decide how to schedule your threads: if you want to encourage your OS to interleave both threads (you can’t force it without more work), try adding sleep or nanosleep or yield calls to your loops (the exact primitives will depend on your platform).


    If it helps you build an intuition about how and why a kernel will make scheduling decisions, note that most CPU architectures will keep a significant amount of state (branch prediction tables, data and instruction caches) that is really good at optimizing a single thread of execution.

    Therefore, it’s generally more efficient to let a given thread run on a given core for as long as possible, to minimize the number of avoidable context switches, cache misses and mis-predictions.

    Now, timeslicing is often used as a sort of tradeoff between the best throughput for each individual process, and the best latency or responsiveness to external events. A thread may block (by waiting for an external event such as user input or device I/O, because it explicitly synchronizes with another thread, or explicitly sleeps or yields), in which case another thread will be scheduled while the first can’t make progress, but otherwise it will typically run until the kernel pre-empts it at the end of its allotted time slice.

    When the parent thread creates a child thread, I wouldn’t like to guess which is “hotter” on the current core, so letting the parent finish its timeslice (unless it blocks) is a reasonable default.

    The child thread is probably runnable right away, but if it doesn’t pre-empt the parent thread, it isn’t obvious why it should immediately pre-empt a thread on a different core either. After all, it’s still in the same process as the parent thread, and shares the same memory, address maps and other resources: unless another core is completely idle, the best place to schedule the child is probably on the same core as its parent, because there’s a decent chance the parent kept those shared resources warm in the cache there.

    So, the reason your threads don’t get interleaved is likely that neither runs for an appreciable fraction of a timeslice before the process exits, and neither does any blocking I/O or explicitly yields (stdout isn’t blocking for that amount of data, as it’ll easily be buffered).

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.