C pthread 和 struct 问题
该代码的基本功能是获取计数器和线程的数量,创建计数器然后创建线程,然后获取每个线程中的指令数量(指令格式 [counter] [work-function] [repetition] )
/* ============================================================================
* File-global variables
* ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;
static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;
struct counter {
long long counter; /* to store counter */
};
/* counter value */
struct instruction {
struct counter *counter; /* pointer to counter */
int repetitions; /* number of repetitions */
void (*work_fn)(long long *); /* function pointer to work function */
};
/* ============================================================================
* Thread function
* ========================================================================== */
static void *
worker_thread(void *arg) {
(void)arg;
int Tcounter;
int Trepetition;
char Tfuntion;
scanf(" %d %c %d", &Tcounter, &Tfuntion, &Trepetition);
我如何实际存储这三个变量使用struct指令**指令???
return NULL;
}
/* ============================================================================
* Main function
* ========================================================================== */
int
main(void) {
scanf(" %d", &ncounters);
int i;
if(counters = malloc(sizeof(struct counter)*ncounters)){
for( i=0; i < ncounters ;i++){
counters[i].counter = 0;
}
for( i=0; i < ncounters ;i++){
printf("%lld\n", counters[i].counter);
}
}
scanf(" %d", &nthreads);
pthread_t threads[nthreads];
int ninst;
for( i=0; i < nthreads ;i++){
scanf(" %d", &ninst);
ninstructions = ninst;
for( i=0; i < ninstructions ;i++){
pthread_create(&threads[i], NULL, worker_thread, NULL);
}
}
free(counters);
return 0;
}
pthread_create函数正确吗?
int
main(void) {
scanf(" %d", &ncounters);
int i;
if(counters = malloc(sizeof(struct counter)*ncounters)){
for( i=0; i < ncounters ;i++){
counters[i].counter = 0;
}
}
scanf(" %d", &nthreads);
pthread_t threads[nthreads];
if(ninstructions = (int*)malloc(nthreads*sizeof(int)){
for( i=0; i < nthreads ;i++){
scanf(" %d", &ninstructions[i]);
int j;
for(j=0; i < ninstructions[i] ;j++){
pthread_create(&threads[j], NULL, worker_thread, NULL);
}
}
}
free(ninstructions);
free(counters);
return 0;
}
我还尝试将指令更改为数组,如果它正确的话......
Basic function of this code is to get number of counter and thread, creates the counters then create the threads then get numbers of instructions in each thread (format of instruction [counter] [work-function] [repetition] )
/* ============================================================================
* File-global variables
* ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;
static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;
struct counter {
long long counter; /* to store counter */
};
/* counter value */
struct instruction {
struct counter *counter; /* pointer to counter */
int repetitions; /* number of repetitions */
void (*work_fn)(long long *); /* function pointer to work function */
};
/* ============================================================================
* Thread function
* ========================================================================== */
static void *
worker_thread(void *arg) {
(void)arg;
int Tcounter;
int Trepetition;
char Tfuntion;
scanf(" %d %c %d", &Tcounter, &Tfuntion, &Trepetition);
How do I actually store this three variable using struct instruction **instructions???
return NULL;
}
/* ============================================================================
* Main function
* ========================================================================== */
int
main(void) {
scanf(" %d", &ncounters);
int i;
if(counters = malloc(sizeof(struct counter)*ncounters)){
for( i=0; i < ncounters ;i++){
counters[i].counter = 0;
}
for( i=0; i < ncounters ;i++){
printf("%lld\n", counters[i].counter);
}
}
scanf(" %d", &nthreads);
pthread_t threads[nthreads];
int ninst;
for( i=0; i < nthreads ;i++){
scanf(" %d", &ninst);
ninstructions = ninst;
for( i=0; i < ninstructions ;i++){
pthread_create(&threads[i], NULL, worker_thread, NULL);
}
}
free(counters);
return 0;
}
Is the pthread_create function correct?
int
main(void) {
scanf(" %d", &ncounters);
int i;
if(counters = malloc(sizeof(struct counter)*ncounters)){
for( i=0; i < ncounters ;i++){
counters[i].counter = 0;
}
}
scanf(" %d", &nthreads);
pthread_t threads[nthreads];
if(ninstructions = (int*)malloc(nthreads*sizeof(int)){
for( i=0; i < nthreads ;i++){
scanf(" %d", &ninstructions[i]);
int j;
for(j=0; i < ninstructions[i] ;j++){
pthread_create(&threads[j], NULL, worker_thread, NULL);
}
}
}
free(ninstructions);
free(counters);
return 0;
}
I also tried to change the ninstruction into array, if its correct...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,下次尝试。这次是伪代码,因为这听起来像家庭作业......
对于受保护的部分,查找互斥体或信号量。
OK, next attempt. This time in pseudo code since this smells like homework...
For protected sections, look up mutex or semaphore.
好的。我假设您想要执行以下操作:
在工作线程中,
我希望这就是您想要的......
Mario
OK. I assume you want to do the following:
And in the worker thread
I hope this is what you wanted...
Mario