user-0 提问: c语言如何创建线程

回复:
user: user-0
content: 引用 12 楼 zhao4zhong1 的回复:连main都拼写错误的人能编好程序吗？ 仅供参考#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32     #include <windows.h>     #include <io.h>     #include <process.h>     #define  MYVOID             void #else     #include <unistd.h>     #include <sys/time.h>     #include <pthread.h>     #define  CRITICAL_SECTION   pthread_mutex_t     #define  _vsnprintf         vsnprintf     #define  MYVOID             void * #endif //Log{ #define MAXLOGSIZE 20000000 #define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) #include <time.h> #include <sys/timeb.h> #include <stdarg.h> char logfilename1[]="MyLog1.log"; char logfilename2[]="MyLog2.log"; char logstr[16000]; char datestr[16]; char timestr[16]; char mss[4]; CRITICAL_SECTION cs_log; FILE *flog; #ifdef WIN32 void Lock(CRITICAL_SECTION *l) {     EnterCriticalSection(l); } void Unlock(CRITICAL_SECTION *l) {     LeaveCriticalSection(l); } void sleep_ms(int ms) {     Sleep(ms); } #else void Lock(CRITICAL_SECTION *l) {     pthread_mutex_lock(l); } void Unlock(CRITICAL_SECTION *l) {     pthread_mutex_unlock(l); } void sleep_ms(int ms) {     usleep(ms*1000); } #endif void LogV(const char *pszFmt,va_list argp) {     struct tm *now;     struct timeb tb;       if (NULL==pszFmt||0==pszFmt[0]) return;     if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,logstr);     flog=fopen(logfilename1,"a");     if (NULL!=flog) {         fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);         if (ftell(flog)>MAXLOGSIZE) {             fclose(flog);             if (rename(logfilename1,logfilename2)) {                 remove(logfilename2);                 rename(logfilename1,logfilename2);             }             flog=fopen(logfilename1,"a");             if (NULL==flog) return;         }         fclose(flog);     } } void Log(const char *pszFmt,...) {     va_list argp;      Lock(&cs_log);     va_start(argp,pszFmt);     LogV(pszFmt,argp);     va_end(argp);     Unlock(&cs_log); } //Log} int No_Loop=0; MYVOID testThread(void *pcn) {     int n,i;      n=(int)pcn;     i=0;     while (1) {         sleep_ms(1000);         Log("in testThread %d:i==%ds\n",n,++i);         if (i>=5) No_Loop=1;     } } int main(int argc,char * argv[]) {     int i; #ifdef WIN32     InitializeCriticalSection(&cs_log); #else     pthread_mutex_init(&cs_log,NULL);     pthread_t threads[1];     int threadsN;     int rc; #endif     Log("=========BEGIN==================\n"); #ifdef WIN32     _beginthread((void(__cdecl *)(void *))testThread,0,(void *)1); #else     threadsN=0;     rc=pthread_create(&(threads[threadsN++]),NULL,testThread,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1); #endif     i=0;     while (1) {         sleep_ms(100);         Log("in main:i==%d\n",++i);         if (No_Loop==1) break;//     }     Log("=========END====================\n"); #ifdef WIN32     DeleteCriticalSection(&cs_log); #else     pthread_mutex_destroy(&cs_log); #endif     return 0; } //2012-06-14 16:27:21.500 =========BEGIN================== //2012-06-14 16:27:21.609 in main:i==1 //2012-06-14 16:27:21.718 in main:i==2 //2012-06-14 16:27:21.828 in main:i==3 //2012-06-14 16:27:21.937 in main:i==4 //2012-06-14 16:27:22.046 in main:i==5 //2012-06-14 16:27:22.156 in main:i==6 //2012-06-14 16:27:22.265 in main:i==7 //2012-06-14 16:27:22.375 in main:i==8 //2012-06-14 16:27:22.484 in main:i==9 //2012-06-14 16:27:22.500 in testThread 1:i==1s //2012-06-14 16:27:22.593 in main:i==10 //2012-06-14 16:27:22.703 in main:i==11 //2012-06-14 16:27:22.812 in main:i==12 //2012-06-14 16:27:22.921 in main:i==13 //2012-06-14 16:27:23.031 in main:i==14 //2012-06-14 16:27:23.140 in main:i==15 //2012-06-14 16:27:23.250 in main:i==16 //2012-06-14 16:27:23.359 in main:i==17 //2012-06-14 16:27:23.468 in main:i==18 //2012-06-14 16:27:23.500 in testThread 1:i==2s //2012-06-14 16:27:23.578 in main:i==19 //2012-06-14 16:27:23.687 in main:i==20 //2012-06-14 16:27:23.796 in main:i==21 //2012-06-14 16:27:23.906 in main:i==22 //2012-06-14 16:27:24.015 in main:i==23 //2012-06-14 16:27:24.125 in main:i==24 //2012-06-14 16:27:24.234 in main:i==25 //2012-06-14 16:27:24.343 in main:i==26 //2012-06-14 16:27:24.453 in main:i==27 //2012-06-14 16:27:24.500 in testThread 1:i==3s //2012-06-14 16:27:24.562 in main:i==28 //2012-06-14 16:27:24.671 in main:i==29 //2012-06-14 16:27:24.781 in main:i==30 //2012-06-14 16:27:24.890 in main:i==31 //2012-06-14 16:27:25.000 in main:i==32 //2012-06-14 16:27:25.109 in main:i==33 //2012-06-14 16:27:25.218 in main:i==34 //2012-06-14 16:27:25.328 in main:i==35 //2012-06-14 16:27:25.437 in main:i==36 //2012-06-14 16:27:25.500 in testThread 1:i==4s //2012-06-14 16:27:25.546 in main:i==37 //2012-06-14 16:27:25.656 in main:i==38 //2012-06-14 16:27:25.765 in main:i==39 //2012-06-14 16:27:25.875 in main:i==40 //2012-06-14 16:27:25.984 in main:i==41 //2012-06-14 16:27:26.093 in main:i==42 //2012-06-14 16:27:26.203 in main:i==43 //2012-06-14 16:27:26.312 in main:i==44 //2012-06-14 16:27:26.421 in main:i==45 //2012-06-14 16:27:26.500 in testThread 1:i==5s //2012-06-14 16:27:26.531 in main:i==46 //2012-06-14 16:27:26.531 =========END====================  我用手机写的

user: user-1
content: 连main都拼写错误的人能编好程序吗？ 仅供参考#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32     #include <windows.h>     #include <io.h>     #include <process.h>     #define  MYVOID             void #else     #include <unistd.h>     #include <sys/time.h>     #include <pthread.h>     #define  CRITICAL_SECTION   pthread_mutex_t     #define  _vsnprintf         vsnprintf     #define  MYVOID             void * #endif //Log{ #define MAXLOGSIZE 20000000 #define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) #include <time.h> #include <sys/timeb.h> #include <stdarg.h> char logfilename1[]="MyLog1.log"; char logfilename2[]="MyLog2.log"; char logstr[16000]; char datestr[16]; char timestr[16]; char mss[4]; CRITICAL_SECTION cs_log; FILE *flog; #ifdef WIN32 void Lock(CRITICAL_SECTION *l) {     EnterCriticalSection(l); } void Unlock(CRITICAL_SECTION *l) {     LeaveCriticalSection(l); } void sleep_ms(int ms) {     Sleep(ms); } #else void Lock(CRITICAL_SECTION *l) {     pthread_mutex_lock(l); } void Unlock(CRITICAL_SECTION *l) {     pthread_mutex_unlock(l); } void sleep_ms(int ms) {     usleep(ms*1000); } #endif void LogV(const char *pszFmt,va_list argp) {     struct tm *now;     struct timeb tb;       if (NULL==pszFmt||0==pszFmt[0]) return;     if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,logstr);     flog=fopen(logfilename1,"a");     if (NULL!=flog) {         fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);         if (ftell(flog)>MAXLOGSIZE) {             fclose(flog);             if (rename(logfilename1,logfilename2)) {                 remove(logfilename2);                 rename(logfilename1,logfilename2);             }             flog=fopen(logfilename1,"a");             if (NULL==flog) return;         }         fclose(flog);     } } void Log(const char *pszFmt,...) {     va_list argp;      Lock(&cs_log);     va_start(argp,pszFmt);     LogV(pszFmt,argp);     va_end(argp);     Unlock(&cs_log); } //Log} int No_Loop=0; MYVOID testThread(void *pcn) {     int n,i;      n=(int)pcn;     i=0;     while (1) {         sleep_ms(1000);         Log("in testThread %d:i==%ds\n",n,++i);         if (i>=5) No_Loop=1;     } } int main(int argc,char * argv[]) {     int i; #ifdef WIN32     InitializeCriticalSection(&cs_log); #else     pthread_mutex_init(&cs_log,NULL);     pthread_t threads[1];     int threadsN;     int rc; #endif     Log("=========BEGIN==================\n"); #ifdef WIN32     _beginthread((void(__cdecl *)(void *))testThread,0,(void *)1); #else     threadsN=0;     rc=pthread_create(&(threads[threadsN++]),NULL,testThread,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1); #endif     i=0;     while (1) {         sleep_ms(100);         Log("in main:i==%d\n",++i);         if (No_Loop==1) break;//     }     Log("=========END====================\n"); #ifdef WIN32     DeleteCriticalSection(&cs_log); #else     pthread_mutex_destroy(&cs_log); #endif     return 0; } //2012-06-14 16:27:21.500 =========BEGIN================== //2012-06-14 16:27:21.609 in main:i==1 //2012-06-14 16:27:21.718 in main:i==2 //2012-06-14 16:27:21.828 in main:i==3 //2012-06-14 16:27:21.937 in main:i==4 //2012-06-14 16:27:22.046 in main:i==5 //2012-06-14 16:27:22.156 in main:i==6 //2012-06-14 16:27:22.265 in main:i==7 //2012-06-14 16:27:22.375 in main:i==8 //2012-06-14 16:27:22.484 in main:i==9 //2012-06-14 16:27:22.500 in testThread 1:i==1s //2012-06-14 16:27:22.593 in main:i==10 //2012-06-14 16:27:22.703 in main:i==11 //2012-06-14 16:27:22.812 in main:i==12 //2012-06-14 16:27:22.921 in main:i==13 //2012-06-14 16:27:23.031 in main:i==14 //2012-06-14 16:27:23.140 in main:i==15 //2012-06-14 16:27:23.250 in main:i==16 //2012-06-14 16:27:23.359 in main:i==17 //2012-06-14 16:27:23.468 in main:i==18 //2012-06-14 16:27:23.500 in testThread 1:i==2s //2012-06-14 16:27:23.578 in main:i==19 //2012-06-14 16:27:23.687 in main:i==20 //2012-06-14 16:27:23.796 in main:i==21 //2012-06-14 16:27:23.906 in main:i==22 //2012-06-14 16:27:24.015 in main:i==23 //2012-06-14 16:27:24.125 in main:i==24 //2012-06-14 16:27:24.234 in main:i==25 //2012-06-14 16:27:24.343 in main:i==26 //2012-06-14 16:27:24.453 in main:i==27 //2012-06-14 16:27:24.500 in testThread 1:i==3s //2012-06-14 16:27:24.562 in main:i==28 //2012-06-14 16:27:24.671 in main:i==29 //2012-06-14 16:27:24.781 in main:i==30 //2012-06-14 16:27:24.890 in main:i==31 //2012-06-14 16:27:25.000 in main:i==32 //2012-06-14 16:27:25.109 in main:i==33 //2012-06-14 16:27:25.218 in main:i==34 //2012-06-14 16:27:25.328 in main:i==35 //2012-06-14 16:27:25.437 in main:i==36 //2012-06-14 16:27:25.500 in testThread 1:i==4s //2012-06-14 16:27:25.546 in main:i==37 //2012-06-14 16:27:25.656 in main:i==38 //2012-06-14 16:27:25.765 in main:i==39 //2012-06-14 16:27:25.875 in main:i==40 //2012-06-14 16:27:25.984 in main:i==41 //2012-06-14 16:27:26.093 in main:i==42 //2012-06-14 16:27:26.203 in main:i==43 //2012-06-14 16:27:26.312 in main:i==44 //2012-06-14 16:27:26.421 in main:i==45 //2012-06-14 16:27:26.500 in testThread 1:i==5s //2012-06-14 16:27:26.531 in main:i==46 //2012-06-14 16:27:26.531 =========END====================

user: user-2
content: 引用 10 楼 fuqingyun 的回复:[quote=引用 9 楼 fly_dragon_fly 的回复:] 硬中断只做简单的事，如果i++和printf相比，那么就应该让中断做i++，windows显示时间并不实时，它仅是一个0.5秒的定时器。也就是线程的消息队列机制， 如果你仅是为了显示变量，内部可以自己实行一个简单的队列来处理，用线程并不能提高什么。 我要的就是定时显示，开始描述不恰当，后面纠正了，定时显示每个一秒一次 [/quote] 线程 是和操作系统相关的，c语言并不存在一个标准的线程操作函数。 如果你的系统是在单片机上面实现的，你应该考虑单片机提供的计时寄存器。 如果你是在windows和linux上面实现的，这个可以用系统提供的线程来实现计时。

user: user-0
content: 引用 9 楼 fly_dragon_fly 的回复:硬中断只做简单的事，如果i++和printf相比，那么就应该让中断做i++，windows显示时间并不实时，它仅是一个0.5秒的定时器。也就是线程的消息队列机制， 如果你仅是为了显示变量，内部可以自己实行一个简单的队列来处理，用线程并不能提高什么。 我要的就是定时显示，开始描述不恰当，后面纠正了，定时显示每个一秒一次

user: user-3
content: 硬中断只做简单的事，如果i++和printf相比，那么就应该让中断做i++，windows显示时间并不实时，它仅是一个0.5秒的定时器。也就是线程的消息队列机制， 如果你仅是为了显示变量，内部可以自己实行一个简单的队列来处理，用线程并不能提高什么。

user: user-0
content: 好吧，再罗嗦一次，i做自增运算，不需要每次自增都显示，也不用传递i值给系统函数，只有当一秒结束的时候，硬件发出int中断指令（汇编可以实现吧），然后转到中段程序，显示i，回复现场，继续运行主程序，直到下一秒结束

user: user-0
content: 中段是指令是时钟发出的，如果不用汇编，有没有现成的可以调用？我好像罗嗦了

user: user-4
content: man pthread_create

user: user-0
content: 从底层考虑，我要求有一个时钟中断程序，隔一秒中断一次，显示参数，假设显示的时间是1/100秒，中断和现场回复的时间几乎可以忽略，那我的程序速度，仅仅降低了1/100而已，但是c能操作中段这么底层的东西吗？如果不能，要实现这样的效果，最好的办法是什么？  再强调:我并不要求每次i++都显示，只是要求每隔一秒显示一次，就显示“当下”的i，而其他时间，程序都在正常运行，

user: user-5
content: 在process.h头文件里  有一个_beginthread函数可以创建线程

user: user-6
content: 可以用线程做，网上搜一下，很多资料的

user: user-7
content: for reference only/*************************************************************************     > File Name: test1.c     > Author: jukay     > Mail: hellojukay@163.com      > Created Time: 2014年12月23日 星期二 20时39分21秒  ************************************************************************/  #include<pthread.h> #include<stdio.h> #include<stdlib.h>  void thread(void) { 	int i; 	for(i = 0; i < 3; i++) 	{ 		printf("This is a pthread!\n"); 	} } int main(void) { 	pthread_t id; 	int i,ret; 	ret = pthread_create(&id,NULL,(void*)thread,NULL); 	if(ret != 0) 	{ 		fprintf(stderr,"create pthread error!\n"); 		exit(EXIT_FAILURE); 	} 	for(i = 0; i < 3; ++i) 		printf("This is a the main process\n");  	pthread_join(id,NULL);  	exit(EXIT_SUCCESS); }

user: user-8
content: 看你线程创建成功否。而且就算是多线程，CPU其实也是按调度方式分别执行，不是真正的并行运行啦，你可以在线程里面输出试试。  CreateThread(NULL,0,ThreadProc,NULL,NULL,NULL);  第一个参数是指向安全属性的指针，NULL就行 第二个参数是初始线程堆栈大小，0就可以，WINDOWS会自动分配 第三个参数指向线程函数，必须是这样DWORD WINAPI ThreadProc (LPVOID pParam)  第四个参数是传给线程函数的参数，有就传，没有就NULL 第五个参数是线程启动方式，可以是挂起CREATE_SUSPENDED，或者马上启动NULL 最后一个参数是创建的线程ID。  成功的话，返回线程句柄。

user: user-0
content: 或者我把问题改一改，我想即时显示程序的某个变量，程序实现或者交给系统去代办都行，就像windows显示时间那样