Best programming approach/methodology to assure thread safety

前端 未结 15 1692
一整个雨季
一整个雨季 2021-01-31 06:28

When I was learning Java coming from a background of some 20 years of procedural programming with basic, Pascal, COBOL and C, I thought at the time that the hardest thing about

15条回答
  •  爱一瞬间的悲伤
    2021-01-31 07:13

    The actor model is what you are using and it is by far the simplest (and efficient way) for multithreading stuff. Basically each thread has a (synchronized) queue (it can be OS dependent or not) and other threads generate messages and put them in the queue of the thread that will handle the message.

    Basic example:

    thread1_proc() {
    
    msg = get_queue1_msg(); // block until message is put to queue1
    threat1_msg(msg);
    
    }
    
    thread2_proc() {
    msg = create_msg_for_thread1();
    send_to_queue1(msg);
    }
    

    It is a tipical example of producer consumer problem.

提交回复
热议问题