这种情况下如何设计线程同步

发布于 2024-12-27 20:08:36 字数 914 浏览 5 评论 0原文

我的需求如下

  1. 有一个多线程的进程。
  2. 其中一个线程 (T1) 由用户事件触发
  3. 有一项任务需要在单独的线程 (T2) 中完成,该线程应由 T1 生成
  4. 现在,T1 应检查系统是否尚未执行以下操作T2 的任务。如果不是,那么它应该生成 T2 然后退出。如果 T2 仍在运行,那么我应该通过记录错误从 T1 返回。我不想在 T2 完成之前保留 T1。
  5. T2通常需要很长时间。因此,如果 T1 在 T2 完成之前被触发,它应该返回一个错误。
  6. 目的是在任何情况下我们都不应该有两个 T2 线程,

我使用互斥体和信号量来执行此操作,但可能有更简单的方法。 这就是我所做的。

Mutex  g_mutex;
Semaphore  g_semaphone;

T1:

if TryLock(g_mutex) succeeds // this means T2 is not active.
spawn T2
else  // This means T2 is currently doing something
return with an error.
wait (g_sempahore) // I come here only if I have spawned the thread. now i wait for T2 to pick the task
// I am here means T2 has picked the task, and I can exit.

T2:

Lock(g_mutex)
signal(g_semaphore)
Do the long task
Unlock(g_mutex)

这很好用。但我想知道是否有更简单的方法可以做到这一点。

My requirement is as follows

  1. There is a process with multiple threads.
  2. One of the threads (T1), gets triggered by a user event
  3. There is a task that needs to be done in a separate thread(T2), which should be spawned by T1
  4. Now, T1 should check that the system is not already doing the task in T2. If its not, then it should spawn T2 and then exit. If T2 is still running, then I should just return from T1 by logging an error. I do not want to hold T1 until T2 is complete.
  5. T2 will usually take a long time. So in case T1 is triggered before T2 has finished it should just return with an error.
  6. The intention is under no circumstance we should have two threads of T2

I am using a mutex and semaphore to do this, but there may be a simpler way.
Here is what i do.

Mutex  g_mutex;
Semaphore  g_semaphone;

T1:

if TryLock(g_mutex) succeeds // this means T2 is not active.
spawn T2
else  // This means T2 is currently doing something
return with an error.
wait (g_sempahore) // I come here only if I have spawned the thread. now i wait for T2 to pick the task
// I am here means T2 has picked the task, and I can exit.

T2:

Lock(g_mutex)
signal(g_semaphore)
Do the long task
Unlock(g_mutex)

And this works fine. But I want to know if there is a simpler way of doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2025-01-03 20:08:36

不要使用这样的互斥体。互斥锁的持有时间应尽可能短。在这种情况下,有一个布尔标志t2_running,它受到互斥锁的保护。在 T1 中执行以下操作:

  1. 锁定 g_mutex
  2. 读取 t2_running
  3. 如果设置了 t2_running,则解锁 g_mutex 并退出并返回错误
  4. Set t2_running
  5. 解锁 g_mutex
  6. 为 T2 填充数据
  7. 生成 T2
  8. 等待 g_semaphore
  9. 成功退出

然后 T2 可以执行以下操作:

  1. 读取数据
  2. 信号 g_semaphore
  3. 处理数据
  4. g_mutex
  5. 清除 t2_running
  6. 解锁 g_mutex
  7. 退出

Do not use a mutex like this. Mutex locks should be held for the minimum time necessary. In this case, have a boolean flag t2_running, which is protected by the mutex. In T1 do:

  1. lock g_mutex
  2. Read t2_running
  3. If t2_running was set, unlock g_mutex and exit with error
  4. Set t2_running
  5. Unlock g_mutex
  6. Populate data for T2
  7. Spawn T2
  8. Wait for g_semaphore
  9. Exit with success

T2 can then do:

  1. Read the data
  2. signal g_semaphore
  3. Process the data
  4. lock g_mutex
  5. Clear t2_running
  6. Unlock g_mutex
  7. exit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文