使用线程同步打印数字的顺序
在这里,我们将看到如何使用不同的线程按正确的顺序打印数字。在这里,我们将创建n个线程,然后对它们进行同步。思路是,第一个线程将打印1,然后第二个线程将打印2,依此类推。当一个线程尝试打印时,它将锁定资源,因此其他线程无法使用该部分。
示例
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t* cond = NULL; int threads; volatile int count = 0; void* sync_thread(void* num) { //this function is used to synchronize the threads int thread_number = *(int*)num; while (1) { pthread_mutex_lock(&mutex); //lock the section if (thread_number != count) { //if the thread number is not same as count, put all thread except one into waiting state pthread_cond_wait(&cond[thread_number], &mutex); } printf("%d ", thread_number + 1); //print the thread number count = (count+1)%(threads); // notify the next thread pthread_cond_signal(&cond[count]); pthread_mutex_unlock(&mutex); } return NULL; } int main() { pthread_t* thread_id; volatile int i; int* thread_arr; printf("</p><p>Enter number of threads: "); scanf("%d", &threads); // allocate memory to cond (conditional variable) thread id's and array of size threads cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t) * threads); thread_id = (pthread_t*)malloc(sizeof(pthread_t) * threads); thread_arr = (int*)malloc(sizeof(int) * threads); for (i = 0; i < threads; i++) { //create threads thread_arr[i] = i; pthread_create(&thread_id[i], NULL, sync_thread, (void*)&thread_arr[i]); } // waiting for thread for (i = 0; i < threads; i++) { pthread_join(thread_id[i], NULL); } return 0; }
输出
$ g++ test.cpp -lpthread $ ./a.out Enter number of threads: 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ... ... ...
以上是使用线程同步打印数字的顺序的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

一般来说,我们只需要同时使用耳机或者音响的其中一个设备,但是有些朋友反映在win11系统中,遇到了耳机和音响一起响的问题,其实我们可以在realtek面板中将它关闭,就可以了,下面一起来看一下吧。win11耳机和音响一起响怎么办1、首先在桌面上找到并打开“控制面板”2、进入控制面板,在其中找到并打开“硬件和声音”3、然后再找到一个喇叭图标的“Realtek高清晰音频管理器”4、选择“扬声器”再点击“后面板”进入扬声器设置。5、打开之后我们可以看到设备类型,如果要关闭耳机就取消勾选“耳机”,如果要

当您在您的同步文件夹中发现一个或多个项目与Outlook中的错误消息不匹配时,这可能是因为您更新或取消了会议项目。这种情况下,您会看到一条错误消息,提示您的本地数据版本与远程副本存在冲突。这种情况通常发生在Outlook桌面应用程序中。您同步的文件夹中的一个或多个项目不匹配。若要解决冲突,请打开这些项目,然后重试此操作。修复同步的文件夹中的一个或多个项目不匹配Outlook错误在Outlook桌面版中,当本地日历项与服务器副本发生冲突时,可能会遇到问题。不过,幸运的是,有一些简单的方法可以帮助您

MySQL是一个非常流行的开源关系型数据库管理系统,广泛应用于各种Web应用、企业系统等。在现代业务的应用场景下,大多数的MySQL数据库需要部署在多台服务器上,以提供更高的可用性和性能,这就需要进行MySQL数据的迁移和同步。本文将介绍如何实现多台服务器之间的MySQL数据迁移和同步。一.MySQL数据迁移MySQL数据迁移指的是将MySQL服务器中的数

为避免线程饥饿,可以使用公平锁确保资源公平分配,或设置线程优先级。为解决优先级反转,可使用优先级继承,即暂时提高持有资源线程的优先级;或使用锁的提升,即提升需要资源线程的优先级。

C++中线程终止和取消机制包括:线程终止:std::thread::join()阻塞当前线程直到目标线程完成执行;std::thread::detach()从线程管理中分离目标线程。线程取消:std::thread::request_termination()请求目标线程终止执行;std::thread::get_id()获取目标线程ID,可与std::terminate()一起使用,立即终止目标线程。实战中,request_termination()允许线程决定终止时机,join()确保在主线

win10剪贴板有个非常好用的功能就是跨设备云储存功能,非常的好用可以帮助用户PC设备和手机设备同步复制黏贴。设置的方法非常简单,只要在系统里的剪切板设置就好。win10剪贴板同步到手机1、首先点击左下角的开始,进入设置。2、然后去点击“系统”。3、选择左侧的“剪贴板”。4、最后在右边的“跨设备同步”中点击登录,然后选择手机就好了。

在进行JavaFX应用程序开发的过程中,我们常常会遇到JavaFX线程卡顿错误。这种错误的严重程度不同,可能会对程序的稳定性和性能产生不利的影响。为了保证程序的正常运行,我们需要了解JavaFX线程卡顿错误的原因和解决方法,以及如何预防这种错误的发生。一、JavaFX线程卡顿错误的原因JavaFX是一个多线程的UI应用程序框架,它允许程序在后台线程中执行长时

百度云同步盘怎么同步?百度云同步盘中可以选择文件来同步,但是多数的用户不知道如何同步百度云文件,接下来就是小编为用户带来的百度云同步盘同步方法图文教程,感兴趣的用户快来一起看看吧!百度云同步盘怎么同步1、首先进入电脑桌面,右键点击【百度云同步盘】图标,选择【设置】;2、之后展开服务小窗口,切换到【高级设置】页面点击【选择文件夹】;3、最后切换到下图的页面,勾选需要同步的文件点击【确定】即可。
