未从 pthread 调用的函数获取输出
我正在尝试学习Pthreads,这是我大学的C模块的一部分。我创建了一小部分代码,以验证正在创建Pthreads并返回一些数据。 请忽略程序中的所有评论。它们是让我想起事情的随机笔记。
此程序编译。但不会产生任何输出(在这种情况下,tid& i)。我期望的是,当我将其扔进命令行参数时。它用于线程数。
请参阅下面的代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//struct
struct threadArgs{
int start;
int finish;
};
// first part - create a function that takes two arguments.
void *threadMain(void *p){
int i,c;
struct threadArgs *pargs = p;
// variables here for leibniz
int nstart=pargs->start, nfinish=pargs->finish;
pthread_t tid = pthread_self(); //The pthread_self() function returns the ID of the calling thread
// i++ is slower than ++i. The difference is in order of operations. i++ copies and increases then throws away. ++i returns the value, and then increments it.
for(i=nstart; i<=nfinish; i++)
{
for(c=2; c<=i-1; c++)
{
if ( i%c==0 )
break;
}
if ( c==i )
printf("Thread %ld : %d\n",tid, i);
}
return 0;
}
int main(int argc, char **argv){
int numThreads = 0;
int i;
pthread_t thrID[100];
struct threadArgs targs[100];
if ( argc > 1 ) {
numThreads = atoi(argv[1]);
}
if (numThreads > 0 && numThreads <= 100){
int chunksize = 10000/numThreads ;
for (i=0; i < numThreads; i++){
targs[i].start = i * chunksize;
targs[i].finish = (i * chunksize) + chunksize;
pthread_create(&thrID[i], NULL, threadMain, &targs[i]);
}
for (i=0; i < numThreads; i++){
pthread_join(thrID[i], NULL);
}
}
}
从此看到的输出...嗯,没有一个。该程序才完成。
谢谢。
I am trying to learn pthreads as part of my C module for university. I have created a small bit of code to just verify that the pthreads are being created and it returning some data. Please ignore all comments in the program. They are random notes to remind me of things.
This program compiles. But does not produce any output (in this case tid & i). what I am expecting, is that when I throw it a command line argument. It is used for number of threads.
see below for the code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//struct
struct threadArgs{
int start;
int finish;
};
// first part - create a function that takes two arguments.
void *threadMain(void *p){
int i,c;
struct threadArgs *pargs = p;
// variables here for leibniz
int nstart=pargs->start, nfinish=pargs->finish;
pthread_t tid = pthread_self(); //The pthread_self() function returns the ID of the calling thread
// i++ is slower than ++i. The difference is in order of operations. i++ copies and increases then throws away. ++i returns the value, and then increments it.
for(i=nstart; i<=nfinish; i++)
{
for(c=2; c<=i-1; c++)
{
if ( i%c==0 )
break;
}
if ( c==i )
printf("Thread %ld : %d\n",tid, i);
}
return 0;
}
int main(int argc, char **argv){
int numThreads = 0;
int i;
pthread_t thrID[100];
struct threadArgs targs[100];
if ( argc > 1 ) {
numThreads = atoi(argv[1]);
}
if (numThreads > 0 && numThreads <= 100){
int chunksize = 10000/numThreads ;
for (i=0; i < numThreads; i++){
targs[i].start = i * chunksize;
targs[i].finish = (i * chunksize) + chunksize;
pthread_create(&thrID[i], NULL, threadMain, &targs[i]);
}
for (i=0; i < numThreads; i++){
pthread_join(thrID[i], NULL);
}
}
}
The output seen from this... well there isn't one. The program just finishes.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论