How much overhead is there when creating a thread?

后端 未结 11 1386
故里飘歌
故里飘歌 2020-12-02 20:14

I just reviewed some really terrible code - code that sends messages on a serial port by creating a new thread to package and assemble the message in a new thread for every

11条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 20:22

    You definitely do not want to do this. Create a single thread or a pool of threads and just signal when messages are available. Upon receiving the signal, the thread can perform any necessary message processing.

    In terms of overhead, thread creation/destruction, especially on Windows, is fairly expensive. Somewhere on the order of tens of microseconds, to be specific. It should, for the most part, only be done at the start/end of an app, with the possible exception of dynamically resized thread pools.

提交回复
热议问题