// gcc -pthread th.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *entry_point(void *value) {
int *num = (int *) value;
int nth = *num;
printf("thread%d (id : %ld) are run!\n", nth, pthread_self());
if (*num > 3) {
*num += 400;
pthread_exit(value);
}
if (*num == 3) *num += 200;
long i = 0;
long d = 100000000;
long max = (*num==1)? 100000000000 : 1000000000;
while(i < max) {
if (i%d == 0) printf("msg from thread%d (i = %ld)\n", nth, i);
i++;
}
if (*num == 203) *num += 10;
else if (*num <= 2) *num += 200;
}
int main(int argc, char **argv) {
pthread_t thread1;
pthread_t thread2;
printf("main thread (id : %ld) are run!\n", pthread_self());
// 4 for test thread exit
// 1 for test normal thread
int num1 = 1;
// 3 for test thread change args value without exit
// 2 for cancel first thread
int num2 = 2;
pthread_create(&thread1, NULL, entry_point, &num1);
pthread_create(&thread2, NULL, entry_point, &num2);
//// for join one thread (wait end of this thread for continue)
//pthread_join(thread1, NULL);
while (1) {
if (num2 > 200 && num2 < 400) pthread_cancel(thread1);
if (num2 == 202 || num2 == 213) break;
}
printf("arg thread1 : %d, arg thread2 : %d\n", num1, num2);
printf("close main thread\n");
return EXIT_SUCCESS;
}
main thread (id : 140324940408640) are run!
thread1 (id : 140324940404480) are run!
msg from thread1 (i = 0)
thread2 (id : 140324932011776) are run!
msg from thread2 (i = 0)
msg from thread2 (i = 100000000)
msg from thread1 (i = 100000000)
msg from thread2 (i = 200000000)
msg from thread1 (i = 200000000)
msg from thread2 (i = 300000000)
msg from thread1 (i = 300000000)
msg from thread2 (i = 400000000)
msg from thread1 (i = 400000000)
msg from thread2 (i = 500000000)
msg from thread1 (i = 500000000)
msg from thread2 (i = 600000000)
msg from thread1 (i = 600000000)
msg from thread2 (i = 700000000)
msg from thread1 (i = 700000000)
msg from thread2 (i = 800000000)
msg from thread1 (i = 800000000)
msg from thread2 (i = 900000000)
msg from thread1 (i = 900000000)
arg thread1 : 1, arg thread2 : 202
close main thread
thread1 (id : 140324940404480) are run!
msg from thread1 (i = 0)
thread2 (id : 140324932011776) are run!
msg from thread2 (i = 0)
msg from thread2 (i = 100000000)
msg from thread1 (i = 100000000)
msg from thread2 (i = 200000000)
msg from thread1 (i = 200000000)
msg from thread2 (i = 300000000)
msg from thread1 (i = 300000000)
msg from thread2 (i = 400000000)
msg from thread1 (i = 400000000)
msg from thread2 (i = 500000000)
msg from thread1 (i = 500000000)
msg from thread2 (i = 600000000)
msg from thread1 (i = 600000000)
msg from thread2 (i = 700000000)
msg from thread1 (i = 700000000)
msg from thread2 (i = 800000000)
msg from thread1 (i = 800000000)
msg from thread2 (i = 900000000)
msg from thread1 (i = 900000000)
arg thread1 : 1, arg thread2 : 202
close main thread
#include <stdio.h>
int main(int argc, char const *argv[]) {
#ifdef __MMX__
printf("MMX ");
#endif
#ifdef __SSE__
printf("SSE ");
#endif
#ifdef __SSE2__
printf("SSE2 ");
#endif
#ifdef __SSE3__
printf("SSE3 ");
#endif
#ifdef __SSSE3__
printf("SSSE3 ");
#endif
#ifdef __SSE4__
printf("SSE4 ");
#endif
#ifdef __SSE4_1__
printf("SSE4.1 ");
#endif
#ifdef __SSE4_2__
printf("SSE4.2 ");
#endif
#ifdef __AVX__
printf("AVX ");
#endif
#ifdef __AVX2__
printf("AVX2 ");
#endif
#ifdef __AVX512F__
printf("AVX512F ");
#endif
#ifdef __k8__
printf("k8 ");
#endif
printf("\n");
return 0;
}
#ifndef CUSTOM_TYPES_H
#define CUSTOM_TYPES_H 1
/* Kernel/Rust like int */
typedef signed char s8;
typedef signed short int s16;
typedef signed int s32;
typedef signed long long int s64;
typedef __int128_t s128;
typedef unsigned char u8;
typedef unsigned short int u16;
typedef unsigned int u32;
typedef unsigned long long int u64;
typedef __uint128_t u128;
/* Custom int */
typedef s8 int8;
typedef s16 int16;
typedef s32 int32;
typedef s64 int64;
typedef s128 int128;
typedef u8 uint8;
typedef u16 uint16;
typedef u32 uint32;
typedef u64 uint64;
typedef u128 uint128;
/* Kernel/Rust like floating point */
#if defined(__HAVE_FLOAT16) && __HAVE_FLOAT16 == 1
typedef __fp16 half;
typedef half fp16;
#else
typedef float fp16;
#endif
typedef float fp32;
typedef double fp64;
#if defined(__HAVE_FLOAT128) && __HAVE_FLOAT128 == 1
typedef __float128 fp128;
#else
typedef double fp128;
#endif
/* Custom floating point */
typedef fp16 float16;
typedef fp32 float32;
typedef fp64 float64;
typedef fp128 float128;
#endif /* CUSTOM_TYPES_H */
#include <stdio.h>
struct user_s {
char* name;
int age;
}
typedef struct user_s user_t;
static const user_t users[] = {
{ .name = "toto", .age = 8 },
{ .name = "titi", .age = 14 },
};
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define foreach(obj, objs) \
typeof(objs[0])* obj = objs; \
for(size_t size = ARRAY_SIZE(objs); size > 0; obj++, size--)
int main() {
//typeof(users[0])* user1 = users;
const user_t* user1 = users;
for(size_t size = ARRAY_SIZE(users); size > 0; user1++, size--) {
printf("%s à %dans\n", user1->name, user1->age);
}
foreach(user2, users) {
printf("%s à %dans\n", user2->name, user2->age);
}
return 0;
}