Created
January 30, 2014 13:07
-
-
Save iamleeg/8707968 to your computer and use it in GitHub Desktop.
I'm not convinced I should be allowed to do this, C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
You're building with -std=c99
, don't do that and it works :). The array definition requires std=gnu99
.
You never said anything about GNU stuff :-p Anyway, I changed nThreads into a #define, that works, too.
Blocks are hardly strict C99…
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?