Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I'm not convinced I should be allowed to do this, C.
#import <Foundation/Foundation.h>
#include <pthread.h>
void logExactlyOnce(void)
{
static __thread dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Only done once?");
});
}
void *threadStart(__unused void *unused)
{
pthread_detach(pthread_self());
logExactlyOnce();
return NULL;
}
void makeATonOfThreads()
{
const int nThreads = 10;
pthread_t threads[nThreads] = {0};
for (int i = 0; i < nThreads; i++) {
pthread_t thisThread = threads[i];
__unused int win = pthread_create(&thisThread, NULL, threadStart, NULL);
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool
{
makeATonOfThreads();
sleep(1);
}
}
@uliwitness

This comment has been minimized.

Copy link

commented Jan 30, 2014

Can't make this run. Getting error "Untitled.m:22:23: error: variable-sized object may not be initialized
pthread_t threads[nThreads] = {0};
^~~~~~~~
1 error generated." Hjälp?

@iamleeg

This comment has been minimized.

Copy link
Owner Author

commented Jan 30, 2014

You're building with -std=c99, don't do that and it works :). The array definition requires std=gnu99.

@uliwitness

This comment has been minimized.

Copy link

commented Jan 30, 2014

You never said anything about GNU stuff :-p Anyway, I changed nThreads into a #define, that works, too.

@iamleeg

This comment has been minimized.

Copy link
Owner Author

commented Jan 30, 2014

Blocks are hardly strict C99…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.