编写一个程序,开启3个线程,这3个线程的id分别为a、b、c,每个线程将自己的id在屏幕上打印10遍,要求输出结果必须按abc的顺序显示;如:abcabc….依次递推。 -凯发k8官方网
发布时间:2024/10/14
编程问答
7
豆豆
1、windows
#include
#include
#include // 线程个数
const int thread_num = 3;// 循环次数
const int loop = 10;// 子线程同步事件
handle g_hthreadevent[thread_num];
// 主线程与子线程同步
handle g_semaphore;
int g_count = 0;unsigned int __stdcall threadfunction(void *ppm)
{int num = *(int *)ppm;// 信号量 releasesemaphore(g_semaphore, 1, null);for (int i = 0; i < loop; i ){// 等待该事件有效。waitforsingleobject(g_hthreadevent[num], infinite);g_count ;printf("第%d次 线程id:=,线程打印:%c\n ", g_count, getcurrentthreadid(), num 'a');// 置位下一个事件有效。setevent(g_hthreadevent[(num 1) % thread_num]);}return 0;
}int main(void)
{int i = 0;handle hthreadhandle[thread_num];// 形参1:安全控制,一般为null// 形参2:资源的初始值。// 形参3:最大的资源数量。// 形参4:该信号量的名称。g_semaphore = createsemaphore(null, 0, 1, null); // 当前0个资源,最大允许1个同时访问for (i = 0; i < thread_num; i ){// 形参1:安全控制。// 形参2:该事件是手动复原(true)还是自动复原(false)。// 形参3:指定事件的初始状态。若为true,则为有信号状态,若为false,则为无信号状态。// 形参4:指定该事件的名称。g_hthreadevent[i] = createevent(null, false, false, null);}for (i = 0; i < thread_num; i ){// 创建线程hthreadhandle[i] = (handle)_beginthreadex(nullptr, 0, threadfunction, &i, 0, nullptr);// 每次创建完线程,该信号量都会等待,直至线程执行到释放信号量的代码为止。waitforsingleobject(g_semaphore, infinite);}// 代码执行到这里,说明各个线程都已经执行到waitforsingleobject,在等待确认信号。// 置位线程1的事件。setevent(g_hthreadevent[0]);// 等待所有线程返回。waitformultipleobjects(thread_num, hthreadhandle, true, infinite);for (i = 0; i < thread_num; i ){closehandle(hthreadhandle[i]);closehandle(g_hthreadevent[i]);}closehandle(g_semaphore);system("pause");return 0;
}
2、linux
#include
#include
#include // 标记指定的线程是否处于等待 cond 的状态。
// 1 处于等待 cond 的状态。
// 0 cond 已成立或者处于阻塞状态。
bool g_flag_ca, g_flag_ab, g_flag_bc;
pthread_mutex_t g_mtx = pthread_mutex_initializer;
pthread_cond_t g_condca = pthread_cond_initializer;
pthread_cond_t g_condab = pthread_cond_initializer;
pthread_cond_t g_condbc = pthread_cond_initializer;using printfunc = void *(*)(void *);char g_cinfo[3] = {'a', 'b', 'c'};
char g_showcount = 10;void mycondwait(bool *pflag, pthread_cond_t *pcond, pthread_mutex_t *pmtx);
void mycondsignal(bool *pflag, pthread_cond_t *pcond, pthread_mutex_t *pmtx);void *printa(void *arg);
void *printb(void *arg);
void *printc(void *arg);int main()
{printfunc printfunc[3] = {printa,printb,printc};const size_t threadcount = sizeof(printfunc) / sizeof(printfunc);pthread_t thread_pid[threadcount] = {0};for (size_t i = 0; i < threadcount; i ){if (pthread_create(thread_pid i, nullptr, printfunc[i], (void *)(g_cinfo i)) != 0){std::cout << "failed to create thread." << std::endl;exit(1);}}// 加 sleep(1) 的作用是使三个线程在执行完 sleep(1) 之后,// 均堵塞在 pthread_cond_wait 。等待各自的条件的释放。sleep(1);pthread_mutex_lock(&g_mtx);mycondsignal(&g_flag_ca, &g_condca, &g_mtx);pthread_mutex_unlock(&g_mtx);for (size_t i = 0; i < threadcount; i ){pthread_join(thread_pid[i], nullptr);}return 0;
}void *printa(void *arg)
{char cinfo = *(char *)arg;char showcount = g_showcount;while (showcount--){pthread_mutex_lock(&g_mtx);mycondwait(&g_flag_ca, &g_condca, &g_mtx);std::cout << 10 - showcount << "." << cinfo;mycondsignal(&g_flag_ab, &g_condab, &g_mtx);pthread_mutex_unlock(&g_mtx); // 解的锁是 pthread_cond_wait 满足条件时加的锁。usleep(50);}// 防止线程 c ,在执行 mycondsignal 函数时,由于 g_flag_ca = 0,导致程序执行在 while 循环中。g_flag_ca = true;return nullptr;
}void *printb(void *arg)
{char cinfo = *(char *)arg;char showcount = g_showcount;while (showcount--){pthread_mutex_lock(&g_mtx);mycondwait(&g_flag_ab, &g_condab, &g_mtx);std::cout << cinfo;mycondsignal(&g_flag_bc, &g_condbc, &g_mtx);pthread_mutex_unlock(&g_mtx);usleep(50);}return nullptr;
}void *printc(void *arg)
{char cinfo = *(char *)arg;char showcount = g_showcount;while (showcount--){pthread_mutex_lock(&g_mtx);mycondwait(&g_flag_bc, &g_condbc, &g_mtx);std::cout << cinfo << std::endl;mycondsignal(&g_flag_ca, &g_condca, &g_mtx);pthread_mutex_unlock(&g_mtx);usleep(50);}return nullptr;
}// 标记并使指定的线程处于等待 cond 满足的状态。
void mycondwait(bool *pflag, pthread_cond_t *pcond, pthread_mutex_t *pmtx)
{*pflag = true;pthread_cond_wait(pcond, pmtx);*pflag = false;return;
}// 向指定的线程发出条件允许的通知。
void mycondsignal(bool *pflag, pthread_cond_t *pcond, pthread_mutex_t *pmtx)
{while (!*pflag){// 执行到这里,说明该线程阻塞在 while 循环开头的 pthread_mutex_lock 函数位置。// 需要调用 pthread_mutex_unlock() 函数将其解锁并执行到 pthread_cond_wait() 函数,使之处于等// 待 cond 的状态。pthread_mutex_unlock(&g_mtx);usleep(50);pthread_mutex_lock(&g_mtx);}// 执行到这里,说明指定的线程已经处于等待 cond 满足的状态。pthread_cond_signal(pcond);return;
}
(saw:game over!)
总结
以上是凯发k8官方网为你收集整理的编写一个程序,开启3个线程,这3个线程的id分别为a、b、c,每个线程将自己的id在屏幕上打印10遍,要求输出结果必须按abc的顺序显示;如:abcabc….依次递推。的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。