When Should I Use Threads?

前端 未结 5 716
情深已故
情深已故 2020-12-29 07:31

As far as I\'m concerned, the ideal amount of threads is 3: one for the UI, one for CPU resources, and one for IO resources.

But I\'m probably wrong.

I\'m ju

5条回答
  •  [愿得一人]
    2020-12-29 08:31

    From the SQLite FAQ: "Threads are evil. Avoid Them." Only use them when you absolutely have to.

    If you have to, then take steps to avoid the usual carnage. Use thread pools to execute fine-grained tasks with no interdependencies, using GUI-framework-provided facilities to dispatch outcomes back to the UI. Avoid sharing data between long-running threads; use message queues to pass information between them (and to synchronise).

    A more exotic solution is to use languages such as Erlang that are explicit designed for fine-grained parallelism without sacrificing safety and comprehensibility. Concurrency itself is of fundamental importance to the future of computation; threads are simply a horrible, broken way to express it.

提交回复
热议问题