【发布时间】:2018-01-23 06:38:48
【问题描述】:
我在 SO 上进行了搜索并用谷歌搜索,但我不明白它们的含义。它们是什么以及它们的目的是什么?它们什么时候使用?我认为在现代编程和我这一代人中看到它们可能为时已晚。
其中一些是 AFAIS,
带有 /* ARGSUSED */
的示例代码#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define BUFSIZE 1024
#define TEN_MILLION 10000000L
/* ARGSUSED */
void *threadout(void *args) {
char buffer[BUFSIZE];
char *c;
struct timespec sleeptime;
sleeptime.tv_sec = 0;
sleeptime.tv_nsec = TEN_MILLION;
snprintf(buffer, BUFSIZE, "This is a thread from process %ld\n",
(long)getpid());
c = buffer;
/*****************start of critical section ********************/
while (*c != '\0') {
fputc(*c, stderr);
c++;
nanosleep(&sleeptime, NULL);
}
/*******************end of critical section ********************/
return NULL;
}
int main(int argc, char *argv[]) {
int error;
int i;
int n;
pthread_t *tids;
if (argc != 2){ /* check for valid number of command-line arguments */
fprintf (stderr, "Usage: %s numthreads\n", argv[0]);
return 1;
}
n = atoi(argv[1]);
tids = (pthread_t *)calloc(n, sizeof(pthread_t));
if (tids == NULL) {
perror("Failed to allocate memory for thread IDs");
return 1;
}
for (i = 0; i < n; i++)
if (error = pthread_create(tids+i, NULL, threadout, NULL)) {
fprintf(stderr, "Failed to create thread:%s\n", strerror(error));
return 1;
}
for (i = 0; i < n; i++)
if (error = pthread_join(tids[i], NULL)) {
fprintf(stderr, "Failed to join thread:%s\n", strerror(error));
return 1;
}
return 0;
}
【问题讨论】:
-
它们是您的特定库之类的“#define”标志吗?
-
/* ARGSUSED */似乎抑制了函数参数argsnot used 警告。我想知道为什么代码没有使用void *threadout(void *args) { (void) args;来做同样的事情?