- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有几个问题。这是我第一次真正尝试制作多线程程序。
注意 - 完整程序在页面底部
(编译,使用
g++ -pthread -o <executable file name> <sourcefile>.cpp -fpermissive
)
我使用 Ubuntu Studio 10.10 64 位编译它。
这个程序最大的问题是它给我一个段错误。
好像是我在int main()中注释的那行引起的。如果我评论那条线,它不会给我一个段错误。
为了方便,这里单独使用 int main():
int main()
{
pthread_attr_t attr;
pthread_t threads[30];
/* Initialize mutex and condition variable objects */
pthread_mutex_init(&direction_mutex, NULL);
pthread_mutex_init(&arrive_mutex,NULL);
pthread_cond_init (&count_threshold, NULL);
pthread_cond_init(&arrive_done, NULL);
/*
For portability, explicitly create threads in a joinable state
I'll take your word for it on that one.
*/
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for( int x = 0 ; x < 30 ; x++)
{
long random = rand();
int direction;
if (random < RAND_MAX/2)
{
direction = 0;
}
else
{
direction = 1;
}
directions[x] = direction;
printf("%d",direction);
}
printf("\n");
currdir = directions[0];
for(int j = 0 ; j < 30 ; j++)
{
if(j != 0)
{
pthread_cond_wait(&arrive_done, NULL); // THIS line of code is what is causing the problem
}
pthread_create(&threads[j], &attr, OneCar, (void *)&Thread_IDs[j]);
}
/* Wait for all threads to complete */
for (j = 0; j < 30; j++)
{
pthread_join(threads[j], NULL);
}
printf("test\n");
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&direction_mutex);
pthread_cond_destroy(&count_threshold);
pthread_exit (NULL);
}
如果没有那一行,程序会运行,但问题是,它似乎以线程顺序随机运行。
我试图使用那个互斥锁来阻止 int main() 启动一个新线程直到最后一个线程完成,因为这个程序应该让线程以 FIFO 顺序运行。
如果没有此代码,它的行为会有所不同。
大多数时候它从线程 0 开始,然后转到线程 3,4,有时甚至是 5,然后再回到线程 1。
有时,它从线程 3 开始,然后转到线程 4,然后是线程 0...我不明白为什么要这样做。
每次都是不同的线程执行顺序,但从来都不是需要的 0,1,2,3,4
这是注释掉违规行的输出:
*** Output of program with "pthread_cond_wait(&arrive_done, NULL);" commented out:
101110010101011111010001000010
ArriveBridge(): Car 1 goes accross the bridge
ExitBridge(): car 1 has left the bridge
Arrivebridge(): Thead 0 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 0 goes accross the bridge
ExitBridge(): car 0 has left the bridge
Arrivebridge(): Thead 5 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 5 goes accross the bridge
ExitBridge(): car 5 has left the bridge
Arrivebridge(): Thead 3 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 3 goes accross the bridge
ExitBridge(): car 3 has left the bridge
ArriveBridge(): Car 2 goes accross the bridge
ExitBridge(): car 2 has left the bridge
ArriveBridge(): Car 4 goes accross the bridge
ExitBridge(): car 4 has left the bridge
Arrivebridge(): Thead 6 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 6 goes accross the bridge
ExitBridge(): car 6 has left the bridge
Arrivebridge(): Thead 7 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 7 goes accross the bridge
ExitBridge(): car 7 has left the bridge
Arrivebridge(): Thead 8 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 8 goes accross the bridge
ExitBridge(): car 8 has left the bridge
Arrivebridge(): Thead 9 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 9 goes accross the bridge
ExitBridge(): car 9 has left the bridge
Arrivebridge(): Thead 10 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 10 goes accross the bridge
ExitBridge(): car 10 has left the bridge
Arrivebridge(): Thead 11 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 11 goes accross the bridge
ExitBridge(): car 11 has left the bridge
ArriveBridge(): Car 13 goes accross the bridge
ExitBridge(): car 13 has left the bridge
Arrivebridge(): Thead 12 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 12 goes accross the bridge
ExitBridge(): car 12 has left the bridge
Arrivebridge(): Thead 14 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 14 goes accross the bridge
ExitBridge(): car 14 has left the bridge
ArriveBridge(): Car 15 goes accross the bridge
ExitBridge(): car 15 has left the bridge
ArriveBridge(): Car 16 goes accross the bridge
ExitBridge(): car 16 has left the bridge
Arrivebridge(): Thead 18 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 18 goes accross the bridge
ExitBridge(): car 18 has left the bridge
Arrivebridge(): Thead 17 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 17 goes accross the bridge
ExitBridge(): car 17 has left the bridge
ArriveBridge(): Car 19 goes accross the bridge
ExitBridge(): car 19 has left the bridge
Arrivebridge(): Thead 21 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 21 goes accross the bridge
ExitBridge(): car 21 has left the bridge
ArriveBridge(): Car 20 goes accross the bridge
ExitBridge(): car 20 has left the bridge
ArriveBridge(): Car 22 goes accross the bridge
ExitBridge(): car 22 has left the bridge
Arrivebridge(): Thead 23 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 23 goes accross the bridge
ExitBridge(): car 23 has left the bridge
Arrivebridge(): Thead 24 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 24 goes accross the bridge
ExitBridge(): car 24 has left the bridge
ArriveBridge(): Car 25 goes accross the bridge
ExitBridge(): car 25 has left the bridge
ArriveBridge(): Car 26 goes accross the bridge
ExitBridge(): car 26 has left the bridge
ArriveBridge(): Car 27 goes accross the bridge
ExitBridge(): car 27 has left the bridge
ArriveBridge(): Car 29 goes accross the bridge
ExitBridge(): car 29 has left the bridge
Arrivebridge(): Thead 28 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 28 goes accross the bridge
ExitBridge(): car 28 has left the bridge
test
这是没有注释掉那行的输出
****output of program before commenting pthread_cond_wait(&arrive_done, NULL); out:
101110010101011111010001000010
Segmentation fault
如您所见,在创建任何线程之前,它几乎立即失败。
我试图改进的另一件事是我的 0 和 1 序列不是很随机。有没有更好的方法来生成随机数?它不必非常随机,但这个序列每次都完全相同。
谢谢你的时间
最佳答案
您实际上需要将互斥体传递给 pthread_cond_wait
,您正在传递 NULL
。
对于从 /dev/random
或 /dev/urandom
读取的随机数据(至少在 linux 上)。您也可以尝试:direction = (rand() >> 8) & 1
关于c++ - 线程 : Segmentation fault error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5254403/
我在一个俄罗斯编程论坛上遇到了这个问题,但还没有想出一个优雅的解决方案。 问题: 你有一个包含N个正整数的数组,你需要将它分成M个连续的段,使得最大段的和是可能的最小值。通过段的总数,我的意思是它所有
我正在从文本“从 0 到 1 的操作系统”中学习链接脚本,并且在文本中他们展示了一个使用关键字 PHDRS 的示例; ENTRY(main); PHDRS { headers PT_P
一个小型测试程序在 64 位 Linux 上使用 gfortran (4.4.5) 返回段错误。 n=2_8**22_8 时不存在故障。 gdb 指示在循环的第一次迭代期间函数 mylen 中发生段错
我正在尝试计算一个比率,当我的分子数组充满 0 时它可以工作,但是当我在分子数组中有值时会中断程序。 223 Double_t *ratio_calculations(int bin_numbers,
我正在研究组合分页/分段系统,在我的书中有两种方法: 1.paged segmentation 2.segmented paging 我无法弄清楚两者之间的区别。我认为在分页分段中,段被分成页面,在分
语义分割只是一种重复,还是“语义分割”和“分割”之间有区别? “场景标记”和“场景解析”有区别吗? 像素级分割和像素级分割有什么区别? (附带问题:当您拥有这种逐像素注释时,您是否可以免费获得对象检测
我遇到了一个非常奇怪的行为,我正在遵循在线文档,并且在创建一个段来与我的子段一起使用时,lambda 按以下顺序执行: xray_recorder.begin_segment('segment-nam
我们正在我们的服务器上进行一些负载测试,我正在使用 tshark 将一些数据捕获到 pcap 文件,然后使用 wireshark GUI 通过转到 Analyze -> expert Info 来查看
我是一名高中生,今年开始学习汇编。 我目前正在制作 Pacman 克隆作为我的最终项目。 我遇到的唯一问题是我的代码很大,*.exe 文件几乎有 64KB。 所以我的问题是,如果我转向模型介质,我需要
前言 本系列文章是博主在工作中使用SAM模型时的学习笔记,包含三部分: SAM初步理解,简单介绍模型框架,不涉及细节和代码 SAM细节理解,对各模块结合代码进一步分析 SAM微调实例
我很困惑进程在使用虚拟内存时如何可能出现段错误。据我了解,“虚拟”内存允许进程访问所有可用内存,然后将其映射到“实际”硬件内存。通过这种转换,进程怎么可能尝试访问不允许访问的内存部分? 最佳答案 听起
Tritwise操作(向右旋转和疯狂操作)无法正确运行,并在Malbolge编译器/解释器中引发分段错误。 在看到有关Coding Challenges和Code Golf的惊人答案之后,我决定开始在
只是有关DirectX11镶嵌的问题。 在“船体着色器”中,可以设置的最大镶嵌因子为64(不确定原因)。现在,尽管对于小型飞机已经足够了,但是对于大型飞机来说,这还远远不够,所以我想知道如何渲染大型飞
我需要在 matlab 中手动分割图像。我很想使用“impoly”,但我主要需要分割圆形对象。 我在 Youtube 上的 Ali Pashaei 视频中找到了我需要的东西,在“FlowImagePr
我有下面的代码。当我取消注释 temperature(i,j) = anode_temperature 时,我会出现 SegFault。 forall(i=0:Cells(1), j=0:Cells(
我是 C 编程新手,我的代码中出现了段错误。该程序使用返回函数来询问用户他们的银行帐户中有多少钱。稍后我将添加代码来计算利息。感谢您为我查看此内容,因为我很难找出为什么会出现此错误。 #include
为了测试,我编写了一个代码来计算 #include int main(void) { int p, i, primes[50], index; boo
这个问题已经有答案了: What is a segmentation fault? (17 个回答) 已关闭 5 年前。 最近我开始研究内置函数,但遇到了一个错误,那就是: 为什么我会遇到此程序段错误
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
注意:我们有很多段错误,基本相同 答案,所以我试图将其分解为一个规范的问题,例如 我们有undefined reference。 尽管我们有一个关于what a segmentati
我是一名优秀的程序员,十分优秀!