我正在学习如何在 C 中使用 Pthread。我尝试使用pthread_mutex_lock. 当锁定成功时,应该返回0。但我的程序总是返回 22 - 无效参数。
代码如下:
pthread_mutex_lock用于work_function
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define N 2
void *work_function(void *param);
int count=0;
pthread_mutex_t mut;
int main(int args, char **argv)
{
pthread_t tid[N];
long int iterations = atoi(argv[1]);
pthread_create(&tid[0], NULL, work_function, (void *) iterations);
pthread_create(&tid[1], NULL, work_function, (void *) iterations);
pthread_join(tid[1], NULL);
pthread_join(tid[0], NULL);
if (count != iterations * 2)
printf("Error: %d\n",count);
else
printf("Value as expected: count = %d\n", count);
pthread_exit(NULL);
}
void …Run Code Online (Sandbox Code Playgroud) 我的c头文件在Xcode中有以下错误消息
Redefinition of 'entry'
Run Code Online (Sandbox Code Playgroud)
但是当我gcc在命令行中使用它编译它时,它工作得很好.你们中的任何人都可以解释原因吗?
这是snapshot.h:
#ifndef SNAPSHOT_H
#define SNAPSHOT_H
#define MAX_KEY_LENGTH 16
#define MAX_LINE_LENGTH 1024
typedef struct value value;
typedef struct entry entry;
typedef struct snapshot snapshot;
struct value {
value* prev;
value* next;
int value;
};
// the line below is where the redefinition error appears
struct entry {
entry* prev;
entry* next;
value* values;
char key[MAX_KEY_LENGTH];
};
struct snapshot {
snapshot* prev;
snapshot* next;
entry* entries;
int id;
};
#endif
Run Code Online (Sandbox Code Playgroud)
这是snapshot.c:
#include <stdio.h> …Run Code Online (Sandbox Code Playgroud)