c – pThread Mutex锁定没有全局互斥锁

weixin_38052215 2019-09-12 01:39:39
我看到的所有使用pThread库进行Mutex锁定的教程都使用了Global Mutex Lock: 看到: https://computing.llnl.gov/tutorials/pthreads/#Mutexes http://www.drdobbs.com/cpp/184401518?pgno=3(对于boost :: thread但相同的上下文) 我想要做的是使用Mutex锁定,其中主文件的全局超出了需要锁定变量的函数的范围.这是一个例子: Main.cpp的 int main() { some initilisation code. tree * tree1; *Start thread to visualise tree* (see code below) Mutex Lock: delete tree1; tree1 = newTree(); Mutex Unlock visualiser.cpp visualise(Tree *) { Forever: Mutex Lock: Check for tree change. Update tree image. Display tree image. Mutex Unlock 我想知道这是否可行: >没有全局范围的互斥锁.>如果可能,不将互斥锁传递给可视化功能. 我明白这可能不合理,如果不是我如何使用extern将全局范围变量传递给visualiser.cpp? 另外,如果我需要将互斥量传递给函数,我将如何去做呢?
...全文
53 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38066880 2019-09-12
  • 打赏
  • 举报
回复
是的,如果互斥体在任何线程正在使用它时保持在范围内,则它不必是全局的.你必须告诉第二个线程互斥体的位置,遗憾的是没有办法解决这个问题. 传递它与传递任何其他变量没有什么不同. 因此,只需定义它并在第一个线程中初始化它,然后在创建第二个线程时,将其地址作为线程参数传递. 然后,第二个线程可以使用该地址访问互斥锁. 就你的评论而言,你希望函数既可以用作线程又可以用作普通函数,我只是因为复杂性而避开了这一点. 你可以做的是将大部分工作放入一个普通的函数中,然后使线程函数成为一个简单的包装器.您甚至可以传入一个互斥指针,该指针可以在有效时使用,如果为NULL则不使用. 有关详细信息,请参阅以下完整程序首先,一些支持的东西,需要的标题和日志功能: #include <pthread.h> #include <stdio.h> #include <time.h> static void mylog (int indent, char *s) { int i; time_t now = time (NULL); struct tm *lt = localtime (&now); printf ("%02d:%02d:%02d ", lt->tm_hour, lt->tm_min, lt->tm_sec); putchar ('|'); for (i = 0; i < indent; i++) printf ("%-20s|", ""); printf ("%-20s|", s); for (i = indent + 1; i < 3; i++) printf ("%-20s|", ""); putchar ('\n'); } 接下来,将完成工作的功能.这是以这样的方式构建的,它可以从任何线程调用,如果你想使用它,可以传递一个互斥指针: static void *myfunction (void *ptr) { pthread_mutex_t *pMtx = ptr; mylog (2, "starting"); if (pMtx != NULL) { mylog (2, "locking mutex"); pthread_mutex_lock (pMtx); mylog (2, "locked mutex"); } mylog (2, "sleeping"); sleep (5); mylog (2, "finished sleeping"); if (pMtx != NULL) { mylog (2, "unlocking mutex"); pthread_mutex_unlock (pMtx); } mylog (2, "stopping"); } 然后是一个实际的线程函数,它实际上是上面工作函数的一个薄包装器.请注意,它通过特定于线程的参数接收互斥锁,并将其传递给工作函数: static void *mythread (void *ptr) { mylog (1, "starting"); mylog (1, "call fn with mutex"); myfunction (ptr); mylog (1, "and back"); mylog (1, "stopping"); } 最后,主要功能.这首先在没有互斥锁的情况下调用工作函数,然后创建一个与另一个线程共享的互斥锁: int main (void) { pthread_mutex_t mtx; pthread_t tid1; char buff[100]; printf (" |%-20s|%-20s|%-20s|\n", "main", "thread", "workfn"); printf (" |%-20s|%-20s|%-20s|\n", "====", "======", "======"); mylog (0, "starting"); mylog (0, "call fn, no mutex"); myfunction (NULL); mylog (0, "and back"); mylog (0, "initing mutex"); pthread_mutex_init (&mtx, NULL); mylog (0, "locking mutex"); pthread_mutex_lock (&mtx); mylog (0, "locked mutex"); mylog (0, "starting thead"); pthread_create (&tid1, NULL, mythread, &mtx); mylog (0, "sleeping"); sleep (5); mylog (0, "sleep done"); mylog (0, "unlocking mutex"); pthread_mutex_unlock (&mtx); mylog (0, "joining thread"); pthread_join (tid1, NULL); mylog (0, "joined thread"); mylog (0, "exiting"); return 0; } 您可以在输出中看到代码如何自行排序: |main |thread |workfn | |==== |====== |====== | 15:07:10 |starting | | | 15:07:10 |call fn, no mutex | | | 15:07:10 | | |starting | 15:07:10 | | |sleeping | 15:07:15 | | |finished sleeping | 15:07:15 | | |stopping | 15:07:15 |and back | | | 15:07:15 |initing mutex | | | 15:07:15 |locking mutex | | | 15:07:15 |locked mutex | | | 15:07:15 |starting thead | | | 15:07:15 |sleeping | | | 15:07:15 | |starting | | 15:07:15 | |call fn with mutex | | 15:07:15 | | |starting | 15:07:15 | | |locking mutex | 15:07:20 |sleep done | | | 15:07:20 |unlocking mutex | | | 15:07:20 |joining thread | | | 15:07:20 | | |locked mutex | 15:07:20 | | |sleeping | 15:07:25 | | |finished sleeping | 15:07:25 | | |unlocking mutex | 15:07:25 | | |stopping | 15:07:25 | |and back | | 15:07:25 | |stopping | | 15:07:25 |joined thread | | | 15:07:25 |exiting | | | 请注意,与使用互斥锁的调用相比,没有互斥锁的直接调用如何起作用.

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧