且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从两个不同的线程调用一个函数,并将关键部分放入被调用的函数中

更新时间:2023-11-24 11:46:22

我认为:

1.所有项目都是作为单个进程。

2.在project2和project3之间没有执行其他同步(即,在调用WriteData()时它们不保存任何其他同步对象。)



如果任何一个假设是假的,你可能需要一个不同的解决方案来解决你的问题。



Enter / LeaveCriticalSection API应放在可能的最小代码块。在这种情况下,这将在project1中的WriteData()函数内部。



放置Enter / LeaveCriticalSection调用指示应该调用Initialize / TerminateCriticalSection API的位置。在这种情况下,它们应该放在project1的初始化代码中。如果project1是一个DLL,应该可以将它放在启动/终止进程期间调用的DllMain()函数中。
I assume that:
1. All projects are run as parts of a single process.
2. No other synchronization is performed between project2 and project3 (i.e. they don't hold any other synchronization objects when calling WriteData()).

If either assumption is false, you will probably need a different solution to your problem.

The Enter/LeaveCriticalSection APIs should be placed around the smallest block of code possible. In this case, this would be inside the WriteData() function in project1.

The placing of the Enter/LeaveCriticalSection calls dictates where the Initialize/TerminateCriticalSection APIs should be called. In this case, they should be placed in the initialization code for project1. If project1 is a DLL, it should be possible to place this in the DllMain() function that is called during startup/termination of the process.