Skip to content

Instantly share code, notes, and snippets.

@iamleeg
Created January 30, 2014 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamleeg/8707968 to your computer and use it in GitHub Desktop.
Save iamleeg/8707968 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

iamleeg commented Jan 30, 2014

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

@uliwitness
Copy link

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

@iamleeg
Copy link
Author

iamleeg 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