2 ms·
> You can't easily spawn 10.000 OS threads. Of course you can. Go on, compile and run this program: #include <pthread.h> #include <stdlib.h> void
by pcwalton 9y ago
> You can't easily spawn 10.000 OS threads.
Of course you can. Go on, compile and run this program:
#include <pthread.h>
#include <stdlib.h>
void *f(void *arg) {
pthread_mutex_lock((pthread_mutex_t *)arg);
pthread_mutex_unlock((pthread_mutex_t *)arg);
return NULL;
}
int main() {
pthread_t threads[10000];
pthread_mutex_t mutexes[10000];
for (unsigned i = 0; i < 10000; i++) {
pthread_mutex_init(&mutexes[i], NULL);
pthread_mutex_lock(&mutexes[i]);
pthread_create(&threads[i], NULL, f, &mutexes[i]);
}
for (unsigned i = 0; i < 10000; i++)
pthread_mutex_unlock(&mutexes[i]);
for (unsigned i = 0; i < 10000; i++)
pthread_join(threads[i], NULL);
return 0;
}
Executes in a second for me, even on macOS.