c# - How to use less CPU in a thread (mainly) responsible for publishing -
when using infinite loop trydequeue
queue, uses cpu.
is possible make operation execute user interface notification, when queue not empty? , run event or function?
i reached conclusion in implementation uses zeromq library, in thread responsible publishing, infinite loop can not avoided.
using (var context = new context(1)){ using (socket client = context.socket(sockettype.pub)){ while (!stopallthread){ sqlqueue.trydequeue(out message); if (message != null){ ... } } }
it's not clear api or technology use, guess there should blocking alternative, such
var message = sqlqueue.dequeue();
in general case block thread until message read queue. shall not consume cpu when there nothing read.
if use zeromq (from comment) there blocking call on server (taken here)
// zmq context, server socket using (zmqcontext context = zmqcontext.create()) using (zmqsocket server = context.createsocket(sockettype.rep)) { server.bind("tcp://*:5555"); while (true) { // wait next request client string message = server.receive(encoding.unicode); ...
comments
if change architecture active polling new message push based model rid of problem of high cpu consumption. in push model have 1 thread reading queue. blocks if there no message there. once message read code dispatches event consumer, saying new message read, , here message consumer handle.
have @ blocking collection take
method.
Comments
Post a Comment