Is there significantly more overhead repeatedly launching a thread than just having one running constantly?

I'm not looking for high performance as I am managing lots of TCP socket connections and not a UDP thing, however I don't want to make any clearly bad practice moves as i am being marked however its just on c++ only (threads and net connections aren't taught yet so I get bonus points for the bones).

this is the code im trying to avoid calling in a thread

1
2
3
4
5
6
7
8
void runThread(){

    while(isAlive){

        send();
        receive();
    }
}


when I could repeatedly call this in a thread only when I want


1
2
3
4
5
if(isAlive){

    send();
    recieve()'
} 


Last edited on
First one is faster. When you create a thread, there's a delay before it is *actually* ready to be used. The delay is usually a significant one.

The first thread should NOT busy-wait though, as it will eat a lot of computing power that way.
Use a mutex, they can avoid busy-waits and they don't cost as much as a full thread.
Thanks that's pretty clear, learnt something too.
Yes, creating threads is expensive. Some libraries do what SGH suggested automatically and encapsulate it as thread pools. Basically, you just create a bunch of threads in advance and make them wait. Then other threads can give them tasks asynchronously, which is much faster than creating a thread for each individual task.
what libraries do this automatically? am using SFML.
> what libraries do this automatically?

Using std::async()

The Microsoft library: excellent performance, uses kernel thread pools
The LLVM library (libc++): acceptable performance, uses thread pools simulated in user mode
The GNU library (libstdc++): there is no library support for thread pools

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <thread>
#include <mutex>
#include <set>
#include <future>
#include <vector>
#include <iomanip>
#include <windows.h>
// #include <cstdlib>

void do_work( std::set<DWORD>& thread_ids )
{
    static int soft_thread_number = 1 ;
    static std::mutex lock ;

    const auto hard_thread_id = ::GetCurrentThreadId() ; // std::this_thread::get_id() ;
    {
        std::lock_guard<std::mutex> lock_it(lock) ;
        ++soft_thread_number ;
        std::cout << "soft_thread_number:" << std::setw(3) << soft_thread_number
                  << "  hard_thread_id: " << hard_thread_id << '\n' << std::flush ;
        thread_ids.insert(hard_thread_id) ;
    }
    using namespace std::literals ;
    std::this_thread::sleep_for( 100ms ) ;
}

int main()
{
    SYSTEM_INFO si ;
    ::GetSystemInfo( std::addressof(si) ) ;
    std::cout << "#processors: " << si.dwNumberOfProcessors << '\n' ;
    // std::system( "echo '#processors: ' && cat /proc/cpuinfo | grep processor | wc -l" ) ;

    std::set<DWORD> thread_ids ;
    std::vector< std::future<void> > futures ;
    for( int i = 0 ; i < 30 ; ++i )
    {
        futures.push_back( std::async( std::launch::async, do_work, std::ref(thread_ids) ) ) ;
        using namespace std::literals ;
        std::this_thread::sleep_for( 10ms ) ;
    }

    for( auto& f : futures ) f.wait() ;
    std::cout << "#unique hard thread ids: " << thread_ids.size() << '\n' ;
}

Microsoft:
#processors: 2
...
#unique hard thread ids: 6

http://rextester.com/LIPR95404

LLVM libc++:
#processors: 
4
...
#unique hard thread ids: 10

GNU libstdc++:
#processors: 
4
...
#unique hard thread ids: 30

http://coliru.stacked-crooked.com/a/fbd4a763b743e08f
why is he reported?
Because some people haven't been brought up properly and consequently antisocial behavior is their only form of communication.
Someone's been going through and reporting him. Some people just don't know how to act mature.
Thankfully, everyone knows how helpful JLBorges is, so nothing will happen to him.
I forgot just how fun you guys were.
Topic archived. No new replies allowed.