我碰到這行代碼今天來用C編程的書的進程和線程章:函數調用之前放置括號的C的結構是什麼?
printf("[Child] child thread id: 0x%x\n", (unsigned int)pthread_self());
我從來沒有見過的部分(unsigned int)pthread_self()
,我不知道是什麼第一對parenthesises的用於。任何想法?
PS:
我記得的PHP文件中,有類似的表達對函數文檔:
int time()
但在實際的代碼中,我們只使用部分time()
,int是爲記錄文件目的以顯示函數的返回值time()
更新:
I型書中的示例代碼,測試每個線程ID:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
int global = 5;
void* ChildCode(void* arg) {
int local = 10;
global++;
local++;
printf("[Child] child thread id: 0x%x\n", (unsigned int)pthread_self());
printf("[Child] global: %d local: %d\n", global, local);
pthread_exit(NULL);
}
int main() {
pthread_t childPid;
int local = 10;
printf("[At start] global: %d local: %d\n", global, local);
/* create a child thread */
if (pthread_create (&childPid, NULL, ChildCode, NULL) != 0)
{
perror("create");
exit(1);
} else { /* parent code */
global++;
local--;
printf("[Parent] parent main thread id : 0x%x\n", (unsigned int)pthread_self());
printf("[Parent] global: %d local: %d\n", global, local);
sleep(1);
}
printf("[At end] global: %d local: %d\n", global, local);
exit(0);
}
,它給了我一些注意事項(不警告沒有錯誤):
clang example_thread.c
/tmp/example_thread-9lEP70.o: In function `main':
example_thread.c:(.text+0xcc): undefined reference to `pthread_create'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不知道代碼,有什麼想法嗎?
您需要將'-pthread'傳遞給clang。見http://stackoverflow.com/questions/2391194/what-is-gs-pthread-equiv-in-clang – ecatmur 2012-08-07 10:21:48
@ecatmur非常感謝,它的作品。 – mko 2012-08-07 10:24:10