How many threads to create and when?

前端 未结 7 1493
不思量自难忘°
不思量自难忘° 2021-01-02 05:37

I have a networking Linux application which receives RTP streams from multiple destinations, does very simple packet modification and then forwards the streams to the final

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-02 06:30

    I'd say, try using just ONE thread; it makes programming much easier. Although you'll need to use something like libevent to multiplex the connections, you won't have any unexpected synchronisation issues.

    Once you've got a working single-threaded implementation, you can do performance testing and make a decision on whether a multi-threaded one is necessary.

    Even if a multithreaded implementation is necessary, it may be easier to break it into several processes instead of threads (i.e. not sharing address space; either fork() or exec multiple copies of the process from a parent) if they don't have a lot of shared data.

    You could also consider using something like Python's "Twisted" to make implementation easier (this is what it's designed for).

    Really there's probably not a good case for using threads over processes - but maybe there is in your case, it's difficult to say. It depends how much data you need to share between threads.

提交回复
热议问题