0

I am new to multithreading ,I read the code like below:

void hello_world(string s)
{
    cout<< s << endl;
}

int main()
{
    const int n = 1000;
    vector<thread> threads;
    for(int i = 0; i < n; i++){
        threads.push_back(thread(hello_world,"test"));
    }   
    for(int i = 0; i < threads.size(); i++){
        threads[i].join();
    }   
    return 0;
}

I believe the program above use 1000 threads to speed up the program,However,this confuses me cause when type the commend lscpu returns:

Thread(s) per core:  2
Core(s) per socket:  6
Socket(s):           1

where I believe the number of threads is 12,Based on description above, I have two questions:

(1) How many threads can I call in one program?

(2) Follow question 1 ,I believe the number of threads we can call is limited,what's the information that I can base on to decide how many thread can I call to make sure other programs run well?

H_E_A_D
  • 101
  • 1
  • 1
  • 8
  • 1
    You are confusing [software threads and hardware threads](https://stackoverflow.com/questions/5593328/software-threads-vs-hardware-threads) – Rachid K. Feb 22 '21 at 09:49
  • Re, "I believe the program above use...threads to speed up the program." That program doesn't do anything else but write "test\n" to the standard output 1000 times. You _can't_ speed that up. The bottleneck is the standard output. All of the output has to go to the same place, and it can only go so fast. Using multple threads either will have no noticeable effect on the overall run time, or more likely, it will actually slow the program down. – Solomon Slow Feb 22 '21 at 13:33

1 Answers1

1

How many threads can I call in one program?

You didn't specify any programming language or operating system but in most cases, there's no hard limit. The most important limit is how much memory is available for the thread stacks. It's harder to quantify how many threads it takes before they all spend more time competing with each other for resources than they spend doing any actual work.

the command lscpu returns: Thread(s) per core: 2, Core(s) per socket: 6, Socket(s): 1

That information tells how many threads the system can simultaneously execute. If your program has more than twelve threads that are ready to run then, at most twelve of them will actually be running at any given point in time, while the rest of them await their turns.

But note: In order for twelve threads to be "ready to run," they have to all be doing tasks that do not interfere with each other. That mostly means, doing computations on values that are already in memory. In your example program, all of the threads want to write to the same output stream. Assuming that the output stream is "thread safe," then that will be something that only one thread can do at a time. It doesn't matter how many cores your computer has.

how many thread can I call to make sure other programs run well?

That's hard to answer unless you know what all of the other programs need to do. And what does "run well" mean, anyway? If you want to be able to use office tools or programming tools to get work done while a large, compute-intensive program runs "in the background," then you'll pretty much have to figure out for yourself just how much work the background program can do while still allowing the "foreground" tools to respond to your typing.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57