- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望用线程打印一些时间(nbFois)。我做到了,但以下代码有问题。仅当所有线程获得相同的 nbFois 时间时,这才有效。
这里使用 nbAffichage[i] = 5 foreach 线程。但如果 nbAffichage 是随机的(例如,第一个是 3...第二个是 6);那么当第一个结束时,第二个就无法开始。
/* Lancer les threads afficheurs */
for (i = 0; i < nbThreads; i++) {
//nbAffichages[i] = rand() % NB_AFFICHAGES;
nbAffichages[i] = 5;
if ((etat = pthread_create(&idThdAfficheurs[i], NULL,
thd_afficher, &nbAffichages[i])) != 0)
thdErreur(etat, "Creation afficheurs", NULL);
}
打印功能
void *thd_afficher (void *arg) {
int i, j, nbLignes;
int *nbFois = (int *)arg;
int monMut=tMut.indiceT[iAffiche];
iAffiche=(iAffiche+1)%nbThreads;
for (i = 0; i < *nbFois; i++) {
nbLignes = rand()% (*nbFois);
//nbLignes = 3;
//l'affichage est trop rapide pour voir la différence
pthread_mutex_lock (&tMut.m[monMut]);//demande accès
for (j = 0; j < nbLignes; j++) {
printf("Thread %lu, j'affiche %d-%d--------%d lignes\n", pthread_self(), i, j,nbLignes);
usleep(10);
}
pthread_mutex_unlock (&tMut.m[(monMut+1)%nbThreads]);//rend accès
}
/* Se terminer sans renvoyer de compte-rendu */
pthread_exit((void *)NULL);
}
全局线程结构
typedef struct ThreadId ThreadId;
struct ThreadId
{
int indiceT[NB_THREADS_MAX];
pthread_mutex_t m[NB_THREADS_MAX];
};
ThreadId tMut;
感谢您的帮助。
编辑:整个代码
/* nbThread affichent un message a l'ecran
Parametre du programme : nbThread
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define NB_THREADS_MAX 20
//#define NB_FOIS 2
typedef struct ThreadId ThreadId;
struct ThreadId
{
int indiceT[NB_THREADS_MAX];
pthread_mutex_t m[NB_THREADS_MAX];
};
ThreadId tMut;//variable de structure avec le tableau de threads et d'indice correespondant
//Mutex ayant le droit d'écrire au début
int iAffiche=0;
int nbThreads=-1;
/*---------------------------------------------------------------------*/
/* Afficher un message d'erreur en fonction du code erreur obtenu
*/
void thdErreur(int codeErr, char *msgErr, void *codeArret) {
fprintf(stderr, "%s: %d soit %s \n", msgErr, codeErr, strerror(codeErr));
pthread_exit(codeArret);
}
/*---------------------------------------------------------------------*/
/* Fonction executee par un thread : afficher un message un certain nombre
de fois nbFois a l'ecran, nbLignes lignes de messages ou nbLignes et
genere aleatoirement
Parametre de creation du thread : nbFois, le nombre d'affichages
*/
void *thd_afficher (void *arg) {
int i, j, nbLignes;
int *nbFois = (int *)arg;
int monMut=tMut.indiceT[iAffiche];
iAffiche=(iAffiche+1)%nbThreads;
for (i = 0; i < *nbFois; i++) {
//nbLignes = rand()% (*nbFois);
nbLignes = 3;
//l'affichage est trop rapide pour voir la différence
pthread_mutex_lock (&tMut.m[monMut]);//demande accès
for (j = 0; j < nbLignes; j++) {
printf("Thread %lu, j'affiche %d-%d--------%d lignes\n", pthread_self(), i, j,nbLignes);
usleep(10);
}
pthread_mutex_unlock (&tMut.m[(monMut+1)%nbThreads]);//rend accès
}
/* Se terminer sans renvoyer de compte-rendu */
pthread_exit((void *)NULL);
}
/*---------------------------------------------------------------------*/
#define NB_AFFICHAGES 10
int main(int argc, char*argv[]) {
pthread_t idThdAfficheurs[NB_THREADS_MAX];
int i, etat;
int nbAffichages[NB_THREADS_MAX];
if (argc != 2) {
printf("Usage : %s <Nb de threads>\n", argv[0]);
exit(1);
}
nbThreads = atoi(argv[1]);
if (nbThreads > NB_THREADS_MAX)
nbThreads = NB_THREADS_MAX;
//initialisation des mutex
for (int k = 0; k < nbThreads; k++){
tMut.indiceT[k]=k;
pthread_mutex_init(&tMut.m[k],NULL);//tout les mutex init à 1
if(k!=0){
pthread_mutex_lock (&tMut.m[k]);//On retire l'accès à tous les mutex sauf le premier
}
}
/* Lancer les threads afficheurs */
for (i = 0; i < nbThreads; i++) {
//nbAffichages[i] = rand() % NB_AFFICHAGES;
nbAffichages[i] = 5;
if ((etat = pthread_create(&idThdAfficheurs[i], NULL,
thd_afficher, &nbAffichages[i])) != 0)
thdErreur(etat, "Creation afficheurs", NULL);
}
/* Attendre la fin des threads afficheur car si le thread principal
- i.e. le main() - se termine, les threads fils crees meurent aussi */
for (i = 0; i < nbThreads; i++)
if ((etat = pthread_join(idThdAfficheurs[i], NULL)) != 0)
thdErreur(etat, "Join threads afficheurs", NULL);
printf ("\nFin de l'execution du thread principal \n");
return 0;
}
最佳答案
您对 iAffiche
的分配存在竞争条件。即使假设一切顺利,您的线程也会锁定“自己的”互斥锁 (monMut
),但随后解锁其邻居的互斥锁!看起来您想要沿线传递一个 token (当线程想要循环不同次数时,这很可能会失败),但您根本不能使用这样的互斥体。
您可以尝试 semaphores 的数组反而。将它们初始化为 0,第一个线程除外,第一个线程为 1。然后,在所有线程中循环相同次数,每个线程等待其信号量,执行其工作(或者如果其 nbAffichages
值太小),然后 ups 下一个线程的信号量。您最终再次处于初始状态,信号量 vector 为 (1,0,0,...)。
关于c - 交替线程打印 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46361212/
我想交织相同模式和相等长度的两个向量。说: a <- rpois(lambda=3,n=5e5) b <- rpois(lambda=4,n=5e5) 我想交织或交织这两个向量,以创建一个等效于c(a
我有两个矩阵,我想以交替方式交织/交错/堆叠在彼此之上/rbind。 ranks=1:3 names=c("Karl", "Klaus", "Mary") x <- cbind(ranks, name
我在 JavaScript 中有一个字符串,如下所示: "This {{@is}} a $|test$| string with $|@string$| delimiters {{as}} follo
那么,我正在尝试“合并”一个字符串 (a) 和一个字符串列表 (b): a = '1234' b = ['+', '-', ''] 获得所需的输出(c): c = '1+2-34' 所需输出字符串中的
我有几个可变长度的数组,其中填充了表示好数据 block 和坏数据 block 的元组。 input = [ [(True, 0, 400), (False, 400, 500), (True
我有一个包含 3 个单元格的表,我想知道是否可以在每个单元格之间“旋转”数据? 基本上,在设定的时间后,我希望第一个单元格中的数据移动到第二个单元格,第二个单元格数据移动到第三个单元格,第三个单元格数
使用 RichFaces dataTable 组件交替行颜色的最简单方法是什么? 最佳答案 查找组件的 rowClasses 属性。 抱歉.. 阅读 richfaces 文档两秒钟后就明白了。 :)
我想创建一个有 4 列的表格,但只显示 3 列。所以我理想情况下有一个跨越 3 列的 div 并应用溢出:隐藏。单击按钮时,我希望第 3 列(公司 2)向左滑动,并替换为第 4 列(公司 3),以便将
有没有办法在 AngularJS 中替代 ng-repeats ? 例如,假设我有两个数组。 数组1 [a1, a2, a3, ...] 数组2 [b1, b2, b3, ...] 我想针对中型和大屏
我有三个包含元素的列表: a = [[0,1],[2,3],...] b = [[5,6],[7,8],...] c = [] 我想将 a 和 b 中的元素 append 到 c 中以获得: c =
我喜欢在 MATLAB 中绘制经过傅立叶变换的信号。通过 set(gca,'xtick',peaks,'FontSize',12);我可以在 x 轴上显示峰值。但有时,峰值靠得太近,显示峰值的文本与其
我正在使用 CSS Grid 为我的网站构建服务列表。整个网站网格中的一行被分成两个 CSS 网格列。 在第一行的第一列中,有一项服务的描述。在第二列中,有一个代表服务的图像。 每一行,描述和图像交替
我有以下 html 代码: 1 2 3 4 5 6 7 8 我想做的是使用奇数/偶数 nth-child 选择器对它们进行一些交替
在下面的代码示例中,我将 window.status 从“a”替换为“b” function alternateViaIntrvl() { setInterval('alterStatus()
下面的CSS和HTML代码生成 News Interviews ---------------------- Djing Break dance ---------------------- 为什么横
我曾经在 tableView willDisplay cell 方法中使用这段代码,但它没有准确地交替颜色 - 它几乎做到了,但有时仍然搞砸了 1 或 2 个相同的颜色和我不确定。 我发现一些建议在我
我的问题的本质是解决方案太多,我想在围绕它构建基础设施之前找出哪一个在优缺点中胜出。 (为了本论坛的目的进行了简化)这是一个拍卖网站,其中五个拍卖按排名#1-5 存储,#1 是当前特色拍卖。其他四个人
如果可能的话,我正在尝试找出是否有一种方法可以替换内容行分隔符的颜色。 例如: 问题是它必须是自动的,所以我假设可能需要 javascript,但我找不到这样的东西。我知道有些事情表明如果你有类似 t
这个看似简单的问题困扰了我整整 10 年。 (好的,不是每天!) 它在 IE 中运行良好,但在 FF 和 Chrome 中运行不正常,这通常表示代码有问题...我想在两侧都有 DIV 框(它们实际上是
我想找到交替 [0, 1, 0, 1] 所需的最少翻转次数,例如给定 [1, 1, 0, 1]。所以在这种情况下,它是一次翻转。 def solution(A): count = 0
我是一名优秀的程序员,十分优秀!